Response
object by using the following methods: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.Response
object.const response = new Response(body?: string | ArrayBuffer | Blob | ReadableStream | null | undefined, init?: ResponseInit);
Parameter | Type | Required | Description |
body | Yes | The body of the Response object. | |
init | No | The initial configuration items of the Response object. |
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. |
// response.bodyreadonly body: ReadableStream;
// response.bodyUsedreadonly bodyUsed: boolean;
// response.headersreadonly headers: Headers;
// response.okreadonly ok: boolean;
// response.statusreadonly status: number;
// response.statusTextreadonly statusText: string;
// response.urlreadonly url: string;
// response.redirectedreadonly redirected: boolean;
// response.redirectUrlsreadonly redirectUrls: Array<String>
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.response.arrayBuffer(): Promise<ArrayBuffer>;
response.blob(): Promise<Blob>;
response.clone(copyHeaders?: boolean): Request;
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. |
response.json(): Promise<object>;
json
.response.text(): Promise<string>;
response.formData(): Promise<FormData>;
Response.error(): Response;
Response.redirect(url: string | URL, status?: number): Response;
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. |
addEventListener('fetch', (event) => {const response = new Response('hello world');event.respondWith(response);});
Was this page helpful?