> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/unjs/ofetch/llms.txt
> Use this file to discover all available pages before exploring further.

# Response Types

> Type definitions for responses and response handling

## Overview

ofetch provides several type definitions for handling different response formats and type-safe response parsing.

## ResponseType

The available response types that ofetch can parse.

```typescript theme={null}
export type ResponseType = keyof ResponseMap | "json";
```

**Available Types:**

* `"json"` - Parse response as JSON (default)
* `"text"` - Return response as text string
* `"blob"` - Return response as Blob
* `"arrayBuffer"` - Return response as ArrayBuffer
* `"stream"` - Return response as ReadableStream

**Example:**

```typescript theme={null}
const text = await ofetch<string>('/file.txt', {
  responseType: 'text'
})

const blob = await ofetch<Blob>('/image.png', {
  responseType: 'blob'
})

const buffer = await ofetch<ArrayBuffer>('/data.bin', {
  responseType: 'arrayBuffer'
})

const stream = await ofetch<ReadableStream>('/large-file', {
  responseType: 'stream'
})
```

## ResponseMap

Maps response type strings to their corresponding TypeScript types.

```typescript theme={null}
export interface ResponseMap {
  blob: Blob;
  text: string;
  arrayBuffer: ArrayBuffer;
  stream: ReadableStream<Uint8Array>;
}
```

This interface is used internally for type inference when you specify a `responseType`.

## MappedResponseType

Utility type that maps a response type string to its corresponding TypeScript type.

```typescript theme={null}
export type MappedResponseType<
  R extends ResponseType,
  JsonType = any,
> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType;
```

**Type Parameters:**

<ParamField path="R" type="ResponseType" required>
  The response type string
</ParamField>

<ParamField path="JsonType" type="any" default="any">
  The TypeScript type to use for JSON responses
</ParamField>

**Example:**

```typescript theme={null}
// MappedResponseType<"text"> = string
// MappedResponseType<"blob"> = Blob
// MappedResponseType<"json", User> = User

const user = await ofetch<User, 'json'>('/api/user')
// Type of user: User

const text = await ofetch<string, 'text'>('/file.txt', {
  responseType: 'text'
})
// Type of text: string
```

## FetchResponse

Extends the standard `Response` interface with an additional `_data` property containing the parsed response data.

```typescript theme={null}
export interface FetchResponse<T> extends Response {
  _data?: T;
}
```

**Properties:**

Inherits all properties from the standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) interface:

<ResponseField name="status" type="number">
  HTTP status code (e.g., 200, 404)
</ResponseField>

<ResponseField name="statusText" type="string">
  HTTP status message (e.g., "OK", "Not Found")
</ResponseField>

<ResponseField name="ok" type="boolean">
  True if status is in the range 200-299
</ResponseField>

<ResponseField name="headers" type="Headers">
  Response headers
</ResponseField>

<ResponseField name="url" type="string">
  Final URL of the response (after redirects)
</ResponseField>

<ResponseField name="redirected" type="boolean">
  True if the response is the result of a redirect
</ResponseField>

<ResponseField name="type" type="ResponseType">
  Type of response (basic, cors, error, etc.)
</ResponseField>

<ResponseField name="body" type="ReadableStream | null">
  Response body stream
</ResponseField>

**Additional Property:**

<ResponseField name="_data" type="T | undefined">
  Parsed response data. The type depends on the `responseType` option.
</ResponseField>

**Example:**

```typescript theme={null}
const response = await ofetch.raw<User[]>('/api/users')

console.log(response.status) // 200
console.log(response.ok) // true
console.log(response._data) // User[] - parsed JSON data
console.log(response.headers.get('content-type')) // application/json
```

## FetchContext

Context object passed to lifecycle hooks containing request, options, response, and error information.

```typescript theme={null}
export interface FetchContext<T = any, R extends ResponseType = ResponseType> {
  request: FetchRequest;
  options: ResolvedFetchOptions<R>;
  response?: FetchResponse<T>;
  error?: Error;
}
```

**Properties:**

<ParamField path="request" type="FetchRequest" required>
  The request URL string or Request object
</ParamField>

<ParamField path="options" type="ResolvedFetchOptions<R>" required>
  Resolved fetch options with headers as a Headers object. See [FetchOptions](/api/options/fetch-options).
</ParamField>

<ParamField path="response" type="FetchResponse<T> | undefined">
  The response object if available. Present in `onResponse` and `onResponseError` hooks.
</ParamField>

<ParamField path="error" type="Error | undefined">
  Error object if an error occurred. Present in `onRequestError` hook.
</ParamField>

**Example:**

```typescript theme={null}
await ofetch('/api/users', {
  onRequest({ request, options }) {
    console.log('Request:', request)
    console.log('Headers:', options.headers)
  },
  onResponse({ response }) {
    console.log('Status:', response?.status)
    console.log('Data:', response?._data)
  },
  onRequestError({ error }) {
    console.error('Error:', error?.message)
  }
})
```

## ResolvedFetchOptions

FetchOptions with headers guaranteed to be a Headers object.

```typescript theme={null}
export interface ResolvedFetchOptions<
  R extends ResponseType = ResponseType,
  T = any,
> extends FetchOptions<R, T> {
  headers: Headers;
}
```

This is the type used in the FetchContext's `options` property, ensuring headers are always in a consistent format.

## Type Parameters

<ParamField path="T" type="any" default="any">
  The expected type of the response data
</ParamField>

<ParamField path="R" type="ResponseType" default="ResponseType">
  The response type: `"json"`, `"text"`, `"blob"`, `"arrayBuffer"`, or `"stream"`
</ParamField>

## Related

* [FetchOptions](/api/options/fetch-options) - Request configuration options
* [FetchHooks](/api/options/hooks) - Lifecycle hooks that use FetchContext
* [ofetch.raw()](/api/ofetch#ofetchraw) - Returns FetchResponse
