Skip to main content
ofetch makes it easy to add custom headers to your requests using the headers option.

Basic Usage

Automatic Headers for JSON

When sending JSON bodies, ofetch automatically sets headers for you:
You can override these defaults:

Merging Headers

Headers are merged when using $fetch.create() or when passing a Request object:

Headers from Request Object

Headers from Request objects are preserved and can be overridden:

Common Patterns

Authentication

API Versioning

Custom User Agent

Content Negotiation

Type Definition

Headers inherit from the standard RequestInit interface and support all native header formats:
Where HeadersInit can be:
  • Headers instance
  • Record<string, string>
  • [string, string][]

Implementation Details

Headers are internally converted to a Headers instance and merged using the mergeHeaders function from src/utils.ts:121-138. Default headers are set at src/fetch.ts:144-153 for JSON-serializable bodies.