# Response

The `Response` interface represents an HTTP response in the [Fetch API](/en/documentation/devtools/runtime/api-reference/fetch/) of Azion Runtime. A function answers a request by returning a `Response` from its [handler](/en/documentation/devtools/runtime/api-reference/handlers/), and `fetch()` resolves to a `Response`. For more information, refer to [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) on MDN Web Docs.

> **Note**
>
> Under `azion dev`, a response that `fetch()` returns reports `type` as `default`, not `basic`, and `redirected` stays `false` after `fetch()` follows a redirect. A response you construct reports the same values in both environments.

---

## Constructor

```javascript
new Response(body, init)
```

| Parameter | Type                                   | Required | Default | Description                                                  |
| --------- | -------------------------------------- | -------- | ------- | ------------------------------------------------------------ |
| `body`    | One of the body types below, or `null` | No       | `null`  | Content of the response body.                                |
| `init`    | Object                                 | No       | `{}`    | Options that set the status and the headers of the response. |

The `body` parameter accepts these types. Three of them set a `content-type` header on the response:

| Body type                                                                             | `content-type` set by the constructor             |
| ------------------------------------------------------------------------------------- | ------------------------------------------------- |
| [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)                       | None                                              |
| `BufferSource`, such as a `Uint8Array`                                                | None                                              |
| [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)               | `multipart/form-data; boundary=<boundary>`        |
| [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)   | None                                              |
| [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) | `application/x-www-form-urlencoded;charset=UTF-8` |
| [`USVString`](https://developer.mozilla.org/en-US/docs/Web/API/USVString) (a string)  | `text/plain;charset=UTF-8`                        |
| `null`                                                                                | None; the body reads as an empty string           |

The `init` object takes these options:

| Option       | Type                                                                                           | Description                                                       |
| ------------ | ---------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| `status`     | Number                                                                                         | Status code of the response, such as `201`.                       |
| `statusText` | String                                                                                         | Status message that goes with the status code, such as `Created`. |
| `headers`    | [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object or a plain object | Headers of the response.                                          |

The constructor ignores a `url` option: the `url` property of a constructed response is an empty string.

---

## Properties

`Response` implements the body of the Fetch API, so it carries `body` and `bodyUsed` beside its own properties:

| Property     | Type             | Description                                                                                                                                         |
| ------------ | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `body`       | `ReadableStream` | Body content, read as a stream. For more information, refer to [ReadableStream](/en/documentation/devtools/runtime/api-reference/readable-stream/). |
| `bodyUsed`   | Boolean          | `true` after the body is read.                                                                                                                      |
| `headers`    | `Headers`        | Headers of the response.                                                                                                                            |
| `ok`         | Boolean          | `true` when `status` is in the range 200–299.                                                                                                       |
| `redirected` | Boolean          | `true` when `fetch()` followed at least one redirect to reach the response.                                                                         |
| `status`     | Number           | Status code of the response, such as `200`.                                                                                                         |
| `statusText` | String           | Status message that goes with the status code, such as `OK` for `200`.                                                                              |
| `type`       | String           | Type of the response: `basic` for a response from `fetch()`, `default` for a response you construct, and `error` for `Response.error()`.            |
| `url`        | String           | Final URL of a response from `fetch()`, after redirects. An empty string on a response you construct.                                               |

---

## Methods

| Method                           | Description                                                                                                 |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `response.clone()`               | Returns a copy of the response. The copy and the original each read the full body.                          |
| `response.json()`                | Reads the body and parses it as JSON. Returns a promise.                                                    |
| `response.text()`                | Reads the body as a string. Returns a promise.                                                              |
| `Response.error()`               | Static. Returns a response with `type` `error` and `status` `0`.                                            |
| `Response.json(data)`            | Static. Returns a response whose body is `data` serialized as JSON, with `content-type` `application/json`. |
| `Response.redirect(url, status)` | Static. Returns a redirect response with the `status` you pass and a `location` header set to `url`.        |

`redirect()` exists only as a static method: call `Response.redirect()`, because a `Response` instance has no `redirect` method.

---

## Example

This handler constructs a response with a status, a status message, a header, and a `url` option, then returns the properties of the response with `Response.json()`:

```javascript
export default {
  async fetch(request, env, ctx) {
    const response = new Response('x', {
      status: 201,
      statusText: 'Created',
      headers: { 'x-h': '1' },
      url: 'https://ignored.example/',
    });
    return Response.json({
      status: response.status,
      statusText: response.statusText,
      ok: response.ok,
      redirected: response.redirected,
      type: response.type,
      url: response.url,
      useFinalURL: response.useFinalURL,
      header: response.headers.get('x-h'),
      bodyType: response.body?.constructor?.name,
      bodyUsed: response.bodyUsed,
    });
  },
};
```

The function returns these values from the response. `"[undefined]"` marks a property that is `undefined`, and `url` is empty because the constructor ignores the `url` option:

```json
{
 "status": 201,
 "statusText": "Created",
 "ok": true,
 "redirected": false,
 "type": "default",
 "url": "",
 "useFinalURL": "[undefined]",
 "header": "1",
 "bodyType": "ReadableStream",
 "bodyUsed": false
}
```

---

## Related resources

- [fetch](/en/documentation/devtools/runtime/api-reference/fetch.md): How a function sends a request and receives a `Response`.
- [Request](/en/documentation/devtools/runtime/api-reference/request.md): The incoming request, and how to build one to pass to `fetch()`.
- [ReadableStream](/en/documentation/devtools/runtime/api-reference/readable-stream.md): How to read or produce a response body as a stream.
- [Web APIs](/en/documentation/devtools/runtime/api-reference/javascript.md): The other Web APIs that Azion Runtime supports.
