Request
The Request interface of Azion Runtime: the incoming request a function receives, its constructor, its read-only properties, and its methods.
The Request interface represents an HTTP request in the Fetch API of Azion Runtime. A function receives each incoming request as a Request: the request argument of a fetch(request, env, ctx) handler, or event.request in a listener that receives a FetchEvent. You also construct a Request to pass to fetch(). For more information, refer to Request on MDN Web Docs.
Constructor
| 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 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. |
bodyUsed | Boolean | false until the body is read, then true. |
headers | Headers | Headers of the request. |
metadata | Object | Metadata of the incoming request, available in a deployed function. For more information, refer to 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 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():
For an incoming GET request, the function returns these values from the request it builds: