Response
The Response API represents the response to an HTTP request. It is designed based on the standard Web API Response.
Note
In Edge Functions, you can obtain a
Response
object by using the following methods:Create a
Response
object by using the Response
constructor API. This object can be used as the value of the response
parameter in the event.respondWith method.Use Fetch to obtain a
Response
object.Constructor API
const response = new Response(body?: string | ArrayBuffer | Blob | ReadableStream | null | undefined, init?: ResponseInit);
Parameters
Parameter | Type | Required | Description |
body | Yes | The body of the Response object. | |
init | No | The initial configuration items of the Response object. |
ResponseInit
Parameter | Type | Required | Description |
status | number | No | The status code for the response. |
statusText | string | No | The status message for the response. The maximum length is 4,095 bytes. If the length exceeds the upper limit, the extra content will be truncated. |
headers | No | The headers associated with the response. |
Attributes
body
// response.bodyreadonly body: ReadableStream;
bodyUsed
// response.bodyUsedreadonly bodyUsed: boolean;
Indicates whether the response body is read.
headers
// response.headersreadonly headers: Headers;
ok
// response.okreadonly ok: boolean;
Indicates whether the response was successful. If the status code ranges from 200 to 299, the response was successful.
status
// response.statusreadonly status: number;
The status code for the response.
statusText
// response.statusTextreadonly statusText: string;
The status message for the response.
url
// response.urlreadonly url: string;
The URL of the response.
redirected
// response.redirectedreadonly redirected: boolean;
Indicates whether the response is the result of a redirect.
redirectUrls
// response.redirectUrlsreadonly redirectUrls: Array<String>
All URLs for redirection.
Methods
Note
If the size of the
HTTP body
obtained by using a method exceeds 1 MB, the OverSize exception will be thrown. In this case, we recommend that you use response.body to read the response body in streaming mode. For more information, see ReadableStream.arrayBuffer
response.arrayBuffer(): Promise<ArrayBuffer>;
The arrayBuffer() method takes a Response stream, reads it to completion, and returns a promise that resolves with an ArrayBuffer.
blob
response.blob(): Promise<Blob>;
The blob() method takes a Response stream, reads it to completion, and returns a promise that resolves with a Blob.
clone
response.clone(copyHeaders?: boolean): Request;
The clone() method creates a clone of a response object.
Parameters
Parameter | Type | Required | Description |
copyHeaders | boolean | No | Specifies whether to copy the response headers of the original object. Default value: false . Valid values:true Copy the response headers of the original object. false Reference the response headers of the original object. |
json
response.json(): Promise<object>;
The json() method takes a Response stream, reads it to completion, and returns a promise which resolves with the parsing result of the body text as
json
.text
response.text(): Promise<string>;
The text() method takes a Response stream, reads it to completion, and returns a promise that resolves with a String.
formData
response.formData(): Promise<FormData>;
The formData() method takes a Response stream, reads it to completion, and returns a promise that resolves with a FormData.
Static Methods
error
Response.error(): Response;
redirect
Response.redirect(url: string | URL, status?: number): Response;
Parameters
Parameter | Type | Required | Description |
url | string | Yes | Redirect URL |
status | number | No | Status code for the response. Valid values: 301, 302, 303, 307, and 308. Default value: 302. |
Sample Code
addEventListener('fetch', (event) => {const response = new Response('hello world');event.respondWith(response);});