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

# request

> API reference for the request fixture, which sends HTTP calls from a test, each one a step in the report, with URLs, timeouts, and cookies.

The `request` fixture sends HTTP requests from a test. A path resolves
against the target's app URL. Each call is a step in the report, and it
ends when the whole response body is read.

```ts title="tests/orders.e2e.ts"

test('creates an order', async ({ request }) => {
  const created = await request.post('/api/orders', { json: { sku: 'A1' } });
  expect(created.status).toBe(201);
  expect(await created.json()).toMatchObject({ sku: 'A1' });
});
```

## Reference

| Method | Returns |
| --- | --- |
| [`fetch`](#fetch) | `Promise<WebResponse>` |
| [`get`](#shorthands) | `Promise<WebResponse>` |
| [`post`](#shorthands) | `Promise<WebResponse>` |
| [`put`](#shorthands) | `Promise<WebResponse>` |
| [`patch`](#shorthands) | `Promise<WebResponse>` |
| [`delete`](#shorthands) | `Promise<WebResponse>` |
| [`head`](#shorthands) | `Promise<WebResponse>` |

Every method takes a URL and [options](#options), and returns a
[`WebResponse`](#webresponse).

### `fetch`

Sends a request with the method in `options.method`, `GET` by default.

```ts
const removed = await request.fetch('/api/orders/7', { method: 'DELETE' });
```

The method is any HTTP method name but `CONNECT`, `TRACE`, and `TRACK`.
e2e sends it upper-cased.

### Shorthands

`get`, `post`, `put`, `patch`, `delete`, and `head` send a request with
their method.

```ts
const found = await request.get('/api/orders?reference=r1');
```

A `GET` or `HEAD` request takes no body.

### Options

| Option | Type | Default |
| --- | --- | --- |
| `headers` | `Record<string, string>` | - |
| `json` | `unknown` | - |
| `body` | `string` | - |
| `timeout` | `number` | `actionTimeout` |
| `redirect` | `'follow' \| 'manual' \| 'error'` | `'follow'` |
| `method` | `string` | `'GET'` |

- `json` is sent as JSON, with `content-type: application/json` unless
  `headers` sets a content type. It takes JSON-safe values only: plain
  objects, arrays, strings, finite numbers, booleans, and `null`. A key whose
  value is `undefined`, a `Date`, and a `secrets.get()` handle are refused.
- `body` is sent as is, with `content-type: text/plain;charset=UTF-8` unless
  `headers` sets one. Pass `json` or `body`, not both. Form, multipart,
  and binary bodies are not supported yet.
- `timeout` is in milliseconds, at most 2147483647. `0` sets no timeout of
  its own, so only the running test's or hook's budget bounds the call.
- `redirect` sets how redirects are handled. `'follow'` follows them;
  `'manual'` returns the 3xx response as is, with its `location` and
  `set-cookie` headers; `'error'` fails the call with `ACTION_FAILED` when
  the server redirects.
- `method` is an option of `fetch` only.

### `WebResponse`

An HTTP response whose body `request` has already read. The web engine's
`route.fetch()` returns the same type.
[`browser.waitForResponse`](https://e2e.com/docs/reference/web#waitforresponse) returns a
`PageResponse`, which extends `WebResponse` with `request`.

| Member | Type |
| --- | --- |
| `status` | `number` |
| `url` | `string` |
| `headers` | `Record<string, string>` |
| `body()` | `Promise<Buffer>` |
| `text()` | `Promise<string>` |
| `json<T>()` | `Promise<T>` |

- `url` is the URL after redirects when `redirect` is `'follow'`, or the
  requested URL when `redirect` is `'manual'`.
- `headers` has lower-cased names. Several `set-cookie` values are joined
  with `\n`. `browser.waitForResponse` leaves cookie headers out.
- `body()`, `text()`, and `json()` wait for the body, and reject with
  `ACTION_FAILED` when it could not be read.
- `text()` decodes the body as UTF-8. `json()` parses it, `unknown` unless
  you name a type. Nothing checks that type: use
  [`toMatchSchema`](https://e2e.com/docs/reference/expect#matching-a-schema) to check it.
- Check a 2xx status with [`toBeOK`](https://e2e.com/docs/reference/expect#tobeok), or an exact
  one with the value matchers: `expect(response.status).toBe(201)`.

## URLs

A path resolves against the target's `app.url` the way
[`app.open`](https://e2e.com/docs/reference/app#open) resolves one. An absolute `http` or `https`
URL goes where it names, on any target.

- A leading `/` starts at the origin. With `app.url` set to
  `https://example.com/shop`, `/api/orders` goes to
  `https://example.com/api/orders`. To keep the base path, write
  ``request.get(`${app.baseUrl}/api/orders`)``.
- The call runs in the runner's process, on the runner's network. An app the
  runner cannot reach, such as one inside a virtual machine the runner only
  drives a browser in, is out of reach for `request` too.
- The web engine's `headers` and `basicAuth` options apply to the browser
  only. Pass the same values in a call's `headers`.

| Throws | When |
| --- | --- |
| `APP_URL_REQUIRED` | The URL is a path and the target declares no `app.url`. |
| `POLICY_DENIED` | The URL is malformed, or has a scheme other than `http` or `https`, `about:blank` included. Also a `Secret` in `json`. |
| `INVALID_ARGUMENT` | The URL is not a non-empty string or has credentials (`https://user:pass@...`), an option has the wrong type or is one `request` does not take (Playwright's `data` or `params`, say), `headers` is not a plain object, a header name or value is invalid, the header is `content-length`, `host`, `sec-fetch-mode`, `expect`, `keep-alive`, `transfer-encoding`, or `upgrade`, a `connection` header other than `close` or `keep-alive`, `accept-encoding` combined with `range`, `json` and `body` are both set, `json` is not JSON-safe, a `GET` or `HEAD` request has a body, `timeout` is negative or over 2147483647, `redirect` is not `'follow'`, `'manual'`, or `'error'`, or the method is not an HTTP method name or is `CONNECT`, `TRACE`, or `TRACK`. |

## Steps and timeouts

Each call is a step of kind `resource`, labeled with the method and the
path: `POST /api/orders`. The label leaves out the query string. A path on
another origin shows the origin too.

A call these checks refuse is still a failed step, and its promise rejects;
the method never throws. When the method or the URL is what was refused, the
step has no label, and no error message quotes the URL or a header value.

A call is bounded by its `timeout`, `actionTimeout` (default 30000 ms) by default, and by the budget of
the code that makes it: the test timeout in a test body, the cleanup timeout
in an `afterEach` hook. When either runs out, e2e aborts the request. A
test that runs out of time fails with its usual timeout error.

| Throws | When |
| --- | --- |
| `ACTION_FAILED` | The call ran past its `timeout`, the server redirected with `redirect: 'error'`, or the network failed: the server refused the connection, or the host does not resolve. |

e2e does not retry a call. To retry a read, wrap it in
[`expect.poll`](https://e2e.com/docs/reference/expect#polling-a-value).

## Cookies and scope

`request` does not send the browser's cookies, and it keeps no cookies
between calls. Pass a token or your app's session cookie in `headers`, as
[Signed-in requests](https://e2e.com/docs/api-testing#signed-in-requests) shows.

`request` is a test fixture. Test bodies, `beforeEach`, and `afterEach` get
it. `beforeAll`, `afterAll`, and worker fixtures do not.

## Types

```ts
export interface APIRequest {
  fetch(url: string, options?: APIRequestFetchOptions): Promise<WebResponse>;
  get(url: string, options?: APIRequestOptions): Promise<WebResponse>;
  post(url: string, options?: APIRequestOptions): Promise<WebResponse>;
  put(url: string, options?: APIRequestOptions): Promise<WebResponse>;
  patch(url: string, options?: APIRequestOptions): Promise<WebResponse>;
  delete(url: string, options?: APIRequestOptions): Promise<WebResponse>;
  head(url: string, options?: APIRequestOptions): Promise<WebResponse>;
}

export interface APIRequestOptions {
  readonly headers?: Readonly<Record<string, string>>;
  readonly json?: unknown;
  readonly body?: string;
  readonly timeout?: number;
  readonly redirect?: 'follow' | 'manual' | 'error';
}

export interface APIRequestFetchOptions extends APIRequestOptions {
  readonly method?: string;
}

export interface WebResponse {
  readonly url: string;
  readonly status: number;
  readonly headers: Readonly<Record<string, string>>;
  body(): Promise<Buffer>;
  json<T = unknown>(): Promise<T>;
  text(): Promise<string>;
}
```

## Related

<CardGroup cols={2}>
  <Card title="API" icon="plug" href="/api-testing">
    Write API tests in the same files and run as UI tests.
  </Card>
  <Card title="expect" icon="circle-check" href="/reference/expect">
    Value matchers for a response's fields and body.
  </Card>
</CardGroup>
