Content-Type header, with smart defaults for JSON content.
Automatic Response Type Detection
ofetch automatically detects the appropriate response type based on theContent-Type header:
Detection Logic
The response type is detected from theContent-Type header:
- JSON:
application/jsonorapplication/*+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-Typeis 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 theresponseType 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.parse().
Text Response
Response.text() method.
Blob Response
Blob object using the Response.blob() method. Useful for binary data like images or files.
ArrayBuffer Response
ArrayBuffer using the Response.arrayBuffer() method. Useful for low-level binary data processing.
Stream Response
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.
Empty Response Bodies
ofetch correctly handles responses that should not have a body:101- Switching Protocols204- No Content205- Reset Content304- Not Modified
Accessing Raw Response
Use$fetch.raw() to access the full Response object along with the parsed data:
_data property of the response object.