Response
The Response interface of Azion Runtime: its constructor, the body types it accepts, its properties, and its instance and static methods.
The Response interface represents an HTTP response in the Fetch API of Azion Runtime. A function answers a request by returning a Response from its handler, and fetch() resolves to a Response. For more information, refer to Response on MDN Web Docs.
Constructor
| 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 | None |
BufferSource, such as a Uint8Array | None |
FormData | multipart/form-data; boundary=<boundary> |
ReadableStream | None |
URLSearchParams | application/x-www-form-urlencoded;charset=UTF-8 |
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 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. |
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():
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: