# Request

The `Request` interface represents an HTTP request in the [Fetch API](/en/documentation/devtools/runtime/api-reference/fetch/) of Azion Runtime. A function receives each incoming request as a `Request`: the `request` argument of a `fetch(request, env, ctx)` [handler](/en/documentation/devtools/runtime/api-reference/handlers/), or `event.request` in a listener that receives a [FetchEvent](/en/documentation/devtools/runtime/api-reference/fetch-event/). You also construct a `Request` to pass to `fetch()`. For more information, refer to [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) on MDN Web Docs.

> **Note**
>
> Under `azion dev`, an assignment to a property of the incoming request is ignored without an error, and `request.metadata` is `undefined`.

---

## Constructor

```javascript
new Request(input, init)
```

| Parameter | Type                | Required | Description                                                                               |
| --------- | ------------------- | -------- | ----------------------------------------------------------------------------------------- |
| `input`   | String or `Request` | Yes      | Resource to request, as a URL or as a `Request` object.                                   |
| `init`    | Object              | No       | Options that set the method, the headers, the body, and the redirect mode of the request. |

The `init` object takes these options:

| Option     | Type                                                                                           | Description                                    |
| ---------- | ---------------------------------------------------------------------------------------------- | ---------------------------------------------- |
| `method`   | String                                                                                         | Request method, such as `GET` or `POST`.       |
| `headers`  | [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object or a plain object | Headers of the request.                        |
| `body`     | String, or the `body` of another request                                                       | Content of the request body.                   |
| `redirect` | String                                                                                         | Redirect mode: `follow`, `error`, or `manual`. |

The constructor normalizes the URL: `new Request('https://example.com')` has the `url` `https://example.com/`.

---

## Properties

| Property   | Type                                                                  | Description                                                                                                                                                                                                                 |
| ---------- | --------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `body`     | `ReadableStream` or `null`                                            | Body content, read as a stream. `null` when the request has no body, such as an incoming `GET` request. For more information, refer to [ReadableStream](/en/documentation/devtools/runtime/api-reference/readable-stream/). |
| `bodyUsed` | Boolean                                                               | `false` until the body is read, then `true`.                                                                                                                                                                                |
| `headers`  | [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) | Headers of the request.                                                                                                                                                                                                     |
| `metadata` | Object                                                                | Metadata of the incoming request, available in a deployed function. For more information, refer to [Metadata](/en/documentation/devtools/runtime/api-reference/metadata/).                                                  |
| `method`   | String                                                                | Request method, such as `GET` or `POST`.                                                                                                                                                                                    |
| `redirect` | String                                                                | Redirect mode: `follow`, `error`, or `manual`. An incoming request has `follow`.                                                                                                                                            |
| `url`      | String                                                                | Full URL of the request, including the query string, such as `https://example.com/a?b=1`.                                                                                                                                   |

Every property of the incoming request is read-only. In a deployed function, an assignment such as `request.url = 'https://example.com/'` throws `TypeError: Cannot set property url of [object Request] which has only a getter`. To change a request, construct a `Request` and pass the values you keep in `init`, as the example below does.

---

## Methods

| Method           | Description                                                                          |
| ---------------- | ------------------------------------------------------------------------------------ |
| `request.text()` | Reads the body as a string. Returns a promise. After the call, `bodyUsed` is `true`. |

A deployed request also carries `arrayBuffer()`, `blob()`, `bytes()`, `clone()`, `formData()`, and `json()`. For their behavior, refer to [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) on MDN Web Docs.

---

## Example

This listener builds a `Request` for `https://example.com` from the method, headers, body, and redirect mode of the incoming request, then returns three properties of the request it built with `Response.json()`:

```javascript
addEventListener("fetch", (event) => {
  const request = event.request;
  const url = "https://example.com";

  const myRequest = new Request(url, {
    body: request.body,
    headers: request.headers,
    method: request.method,
    redirect: request.redirect,
  });

  event.respondWith(
    Response.json({
      method: myRequest.method,
      url: myRequest.url,
      redirect: myRequest.redirect,
    })
  );
});
```

For an incoming `GET` request, the function returns these values from the request it builds:

```json
{
 "method": "GET",
 "url": "https://example.com/",
 "redirect": "follow"
}
```

---

## Related resources

- [fetch](/en/documentation/devtools/runtime/api-reference/fetch.md): How a function sends a `Request` and receives a response.
- [FetchEvent](/en/documentation/devtools/runtime/api-reference/fetch-event.md): The event that carries the incoming request as `event.request`.
- [Response](/en/documentation/devtools/runtime/api-reference/response.md): How to build the response a function returns.
- [Web APIs](/en/documentation/devtools/runtime/api-reference/javascript.md): The other Web APIs that Azion Runtime supports.
