Skip to main content
ofetch automatically handles JSON request bodies for you. When you pass an object or array as the body option, ofetch will:
  1. Stringify it to JSON
  2. Set the content-type header to application/json
  3. Set the accept header to application/json

Basic Usage

Content-Type Detection

ofetch only auto-stringifies when the content is JSON-serializable and you haven’t set a different content-type:

What Gets Stringified?

ofetch considers a value JSON-serializable if it’s:
  • A plain object ({})
  • An array ([])
  • A primitive (string, number, boolean, null)
  • An object with a .toJSON() method
The following are not stringified:
  • FormData - sent as-is
  • URLSearchParams - sent as-is
  • Blob - sent as-is
  • ArrayBuffer / typed arrays - sent as-is
  • ReadableStream - sent as-is

toJSON() Support

If your object has a .toJSON() method, ofetch will automatically call it:

Form URL Encoded

You can send form-encoded data by setting the content-type:

Type Definition

From src/types.ts:27:
The body can be any standard RequestInit body type, or a plain object/array that will be automatically stringified.