# Read the response (https://effetch.monarcode.dev/docs/calls/responses)

Most calls come back with a JSON body that you want as a typed value. Give
the call an `output` schema, and Effetch decodes the body with it, so the
call succeeds with the value your code expects. This page shows how, and
how to read a body that isn't JSON, or the status and headers next to it.

## Decode the body with a schema

Pass the schema as `output`:

```ts
import * as Schema from "effect/Schema";

import type { Fetch } from "@monarcode/effetch";

declare const api: Fetch.Client;

const Todo = Schema.Struct({ id: Schema.Finite, title: Schema.String });

// Succeeds with { id: number, title: string }
export const todo = api.get("/todos/:id", { params: { id: 1 }, output: Todo });
```

The call succeeds with the decoded value, or fails with `DecodeError` if
the body doesn't match. It also sends `Accept: application/json`, unless an
`accept` header is set on the call, the endpoint or the client.

Only a 2xx response is decoded. Any other status, 3xx included, fails with
`HttpError` before `output` runs (see [Call errors](https://effetch.monarcode.dev/docs/errors)). `fetch`
follows redirects itself, so a 3xx only arrives with `redirect: "manual"`
or from a conditional request, such as `304 Not Modified`.

> **Use Schema.Finite for numbers**
>
> Use `Schema.Finite` rather than `Schema.Number` for numbers. Responses are
> decoded with Effect's JSON codec, where `Schema.Number` also decodes the
> strings `"NaN"`, `"Infinity"` and `"-Infinity"`, and encodes those values back
> to strings. `Schema.Finite` accepts every JSON number and rejects the rest.

## Handle an empty body

Some responses have no body, such as a `204 No Content` or any `HEAD`
response. An empty body decodes as JSON `null`, so give such a call
`output: Schema.Void`:

```ts
import * as Schema from "effect/Schema";

import type { Fetch } from "@monarcode/effetch";

declare const api: Fetch.Client;

export const archive = api.post("/todos/:id/archive", {
  params: { id: 1 },
  output: Schema.Void,
});
```

`Schema.Undefined` and `Schema.NullOr(…)` accept an empty body too. A JSON
`null` body and an empty one decode the same way, so a schema can't tell
them apart.

## Skip decoding

Without `output`, the call succeeds with the raw `HttpClientResponse`.
That's handy when you only care that the call worked, or want to read the
body yourself:

```ts
import type { Fetch } from "@monarcode/effetch";

declare const api: Fetch.Client;

// Succeeds with the HttpClientResponse.
export const remove = api.del("/todos/:id", { params: { id: 1 } });
```

The status is still checked: a response outside 2xx fails with `HttpError`
whether or not the call has an `output`.

## Read text or bytes

`output` reads the body as JSON. For other bodies, wrap the schema in one of
these:

- `Fetch.text()` reads the body as a string, as it was sent.
- `Fetch.text(schema)` reads a single value, such as a count or a status. It
  trims the text, then decodes it with a schema whose encoded side is a
  string, such as `Schema.NumberFromString`.
- `Fetch.bytes()` reads the body as a `Uint8Array`, a copy of the body, for
  images, PDFs and other binary bodies.

The following program reads a CSV export as text, a count as a number, and a
PDF as bytes:

```ts
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import { Fetch } from "@monarcode/effetch";

declare const api: Fetch.Client;

export const program = Effect.gen(function* () {
  const csv = yield* api.get("/export.csv", { output: Fetch.text() });
  const count = yield* api.get("/todos/count", {
    output: Fetch.text(Schema.NumberFromString),
  });
  const pdf = yield* api.get("/invoices/:id", {
    params: { id: 7 },
    output: Fetch.bytes(),
  });
  // In a browser, Node, Bun or Deno. React Native's Blob doesn't take arrays.
  return { csv, count, pdf: new Blob([pdf], { type: "application/pdf" }) };
});
```

They work wherever `output` does, endpoints included. The one exception is
`Fetch.bytes()`, which a stream (`api.sse`, `api.ndjson` or a stream
endpoint) can't take.

The following rules apply to `Fetch.text` and `Fetch.bytes`:

- The call sends a matching `Accept` header: `text/*, */*;q=0.1` for text,
  and `*/*` for bytes, unless an `accept` header is set on the call, the
  endpoint or the client.
- An empty body is `""` for `Fetch.text()`, and a `Uint8Array` of length 0
  for `Fetch.bytes()`.
- For `Fetch.text(schema)`, an empty body fails with `DecodeError` before it
  reaches the schema, so `Schema.NumberFromString` never reads it as `0`.
- A body that can't be read, or doesn't match the schema, fails with
  `DecodeError`.
- Error responses are still read as JSON for `error` and `statusErrors`.
- Pass the schema they return to `output` as it is, or with more checks or
  annotations. A schema built around it, such as with `Schema.NullOr`, is
  read as JSON again.

## Read the body and the response

To read the status or headers next to the decoded body, such as a total count
or an `ETag`, set `responseMode: "decoded-and-response"`. The call succeeds
with a pair:

```ts
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import type { Fetch } from "@monarcode/effetch";

declare const api: Fetch.Client;

const Todo = Schema.Struct({ id: Schema.Finite, title: Schema.String });

export const page = Effect.gen(function* () {
  const [todos, response] = yield* api.get("/todos", {
    query: { page: 1 },
    output: Schema.Array(Todo),
    responseMode: "decoded-and-response",
  });
  return { todos, total: Number(response.headers["x-total-count"]) };
});
```

The following rules apply to `responseMode`:

- The default, `"decoded"`, gives the decoded body alone.
- It's allowed on calls, on endpoint definitions, and on each call of an
  endpoint, where the call's own mode wins.
- Without `output`, both items are the response.
- The body has already been read, so use the decoded value rather than
  reading the response's body again.

## What's next

- [Call errors](https://effetch.monarcode.dev/docs/errors): handle `DecodeError` and the other ways a call
  fails.
- [Errors by status](https://effetch.monarcode.dev/docs/errors/status-errors): turn error responses into
  your own errors.
- [Streaming responses](https://effetch.monarcode.dev/docs/streaming): read Server-Sent Events and
  newline-delimited JSON.