Skip to main content

JSON, text, blob, and ArrayBuffer responses

Use case

Control how the response body is parsed. By default parcely returns parsed JSON, but you can request text, binary data, or blobs.

JSON (default)

import { createClient } from '@parcely/core'

const http = createClient({ baseURL: 'https://api.example.com' })

const { data } = await http.get('/users/me')
// data is the parsed JSON object

parcely is content-type-aware: if the response Content-Type is not a JSON type, the body is returned as raw text instead of crashing with a SyntaxError.

Text

const { data } = await http.get('/readme', { responseType: 'text' })
// data is a string

ArrayBuffer

const { data } = await http.get('/image.png', { responseType: 'arraybuffer' })
// data is an ArrayBuffer

Blob

const { data } = await http.get('/document.pdf', { responseType: 'blob' })
// data is a Blob

Axios equivalent

// axios:
const { data } = await http.get('/file', { responseType: 'arraybuffer' })

// parcely (identical):
const { data: d } = await http.get('/file', { responseType: 'arraybuffer' })

Both libraries support 'json', 'text', 'arraybuffer', and 'blob'. parcely also supports 'stream' (returns the un-consumed ReadableStream — see the download streaming guide). Axios supports 'stream' (Node Readable) and 'document' (browser DOM); parcely does not provide 'document'.

Notes and gotchas

  • The responseType type is 'json' | 'text' | 'arraybuffer' | 'blob' | 'stream'.
  • When responseType is 'json' (default), parcely sniffs the Content-Type header. If it does not indicate JSON, the response is returned as text to avoid parse errors.
  • If JSON parsing fails even with a JSON content type, parcely throws HttpError with code: 'ERR_PARSE'.