Skip to main content
ofetch automatically detects and parses responses based on the Content-Type header, with smart defaults for JSON content.

Automatic Response Type Detection

ofetch automatically detects the appropriate response type based on the Content-Type header:

Detection Logic

The response type is detected from the Content-Type header:
  • JSON: application/json or application/*+json (e.g., application/vnd.api+json)
  • Text: text/*, image/svg, application/xml, application/xhtml, application/html
  • Stream: text/event-stream (Server-Sent Events)
  • Blob: All other content types
  • Default: When no Content-Type is present, defaults to JSON
ofetch uses a regex pattern /^application\/(?:[\w!#$%&*.^~-]*+)?json(;.+)?$/ito detect JSON content types, which supports JSON variants likeapplication/vnd.api+json`.

Response Types

You can explicitly specify the response type using the responseType option:
'json' | 'text' | 'blob' | 'arrayBuffer' | 'stream'
The expected response type. When specified, ofetch will use the corresponding method to parse the response.

JSON Response

JSON is the default response type. The response text is parsed using JSON.parse().

Text Response

Returns the response as a string using the Response.text() method.

Blob Response

Returns the response as a Blob object using the Response.blob() method. Useful for binary data like images or files.

ArrayBuffer Response

Returns the response as an ArrayBuffer using the Response.arrayBuffer() method. Useful for low-level binary data processing.

Stream Response

Returns the response as a ReadableStream<Uint8Array>. This is automatically detected for text/event-stream content type.

Custom Response Parsing

You can provide a custom parser function to handle response parsing:
(responseText: string) => any
A custom function to parse the response text. When provided, this overrides the default JSON parsing.
When parseResponse is provided, the responseType option is ignored and the response is always treated as text before being passed to your custom parser.

Empty Response Bodies

ofetch correctly handles responses that should not have a body:
The following status codes are treated as having no body:
  • 101 - Switching Protocols
  • 204 - No Content
  • 205 - Reset Content
  • 304 - Not Modified

Accessing Raw Response

Use $fetch.raw() to access the full Response object along with the parsed data:
The parsed data is available on the _data property of the response object.