TheResponse interface represents an HTTP response and is part of the Fetch API.
letresponse=newResponse(body,init);bodyoptionalAn object that defines the body text for the response. Can be
nullor any one of the following types:- BufferSource
- FormData
- ReadableStream
- URLSearchParams
- USVString
initoptional- An
optionsobject that contains custom settings to apply to the response.
- An
Valid options for theoptions object include:
cfany | null- An object that contains Cloudflare-specific information. This object is not part of the Fetch API standard and is only available in Cloudflare Workers. This field is only used by consumers of the Response for informational purposes and does not have any impact on Workers behavior.
encodeBodystring- Workers have to compress data according to the
content-encodingheader when transmitting, to serve data that is already compressed, this property has to be set to"manual", otherwise the default is"automatic".
- Workers have to compress data according to the
headersHeaders | ByteString- Any headers to add to your response that are contained within a
Headersobject or object literal ofByteString↗ key-value pairs.
- Any headers to add to your response that are contained within a
statusint- The status code for the response, such as
200.
- The status code for the response, such as
statusTextstring- The status message associated with the status code, such as,
OK.
- The status message associated with the status code, such as,
webSocketWebSocket | null- This is present in successful WebSocket handshake responses. For example, if a client sends a WebSocket upgrade request to an origin and a Worker intercepts the request and then forwards it to the origin and the origin replies with a successful WebSocket upgrade response, the Worker sees
response.webSocket. This establishes a WebSocket connection proxied through a Worker. Note that you cannot intercept data flowing over a WebSocket connection.
- This is present in successful WebSocket handshake responses. For example, if a client sends a WebSocket upgrade request to an origin and a Worker intercepts the request and then forwards it to the origin and the origin replies with a successful WebSocket upgrade response, the Worker sees
response.bodyReadable Stream- A getter to get the body contents.
response.bodyUsedboolean- A boolean indicating if the body was used in the response.
response.headersHeaders- The headers for the response.
response.okboolean- A boolean indicating if the response was successful (status in the range
200-299).
- A boolean indicating if the response was successful (status in the range
response.redirectedboolean- A boolean indicating if the response is the result of a redirect. If so, its URL list has more than one entry.
response.statusint- The status code of the response (for example,
200to indicate success).
- The status code of the response (for example,
response.statusTextstring- The status message corresponding to the status code (for example,
OKfor200).
- The status message corresponding to the status code (for example,
response.urlstring- The URL of the response. The value is the final URL obtained after any redirects.
response.webSocketWebSocket?- This is present in successful WebSocket handshake responses. For example, if a client sends a WebSocket upgrade request to an origin and a Worker intercepts the request and then forwards it to the origin and the origin replies with a successful WebSocket upgrade response, the Worker sees
response.webSocket. This establishes a WebSocket connection proxied through a Worker. Note that you cannot intercept data flowing over a WebSocket connection.
- This is present in successful WebSocket handshake responses. For example, if a client sends a WebSocket upgrade request to an origin and a Worker intercepts the request and then forwards it to the origin and the origin replies with a successful WebSocket upgrade response, the Worker sees
clone(): Response- Creates a clone of a
Responseobject.
- Creates a clone of a
json(): Response- Creates a new response with a JSON-serialized payload.
redirect(): Response- Creates a new response with a different URL.
Response implements theBody ↗ mixin of theFetch API ↗, and thereforeResponse instances additionally have the following methods available:
arrayBuffer(): Promise<ArrayBuffer>- Takes a
Responsestream, reads it to completion, and returns a promise that resolves with anArrayBuffer↗.
- Takes a
formData(): Promise<FormData>- Takes a
Responsestream, reads it to completion, and returns a promise that resolves with aFormData↗ object.
- Takes a
json(): Promise<JSON>text(): Promise<USVString>- Takes a
Responsestream, reads it to completion, and returns a promise that resolves with aUSVString↗ (text).
- Takes a
TheContent-Length header will be automatically set by the runtime based on whatever the data source for theResponse is. Any value manually set by user code in theHeaders will be ignored. To have aContent-Length header with a specific value specified, thebody of theResponse must be either aFixedLengthStream or a fixed-length value just as a string orTypedArray.
AFixedLengthStream is an identityTransformStream that permits only a fixed number of bytes to be written to it.
const{writable,readable}=newFixedLengthStream(11);constenc=newTextEncoder();constwriter=writable.getWriter();writer.write(enc.encode("hello world"));writer.end();returnnewResponse(readable);Using any other type ofReadableStream as the body of a response will result in chunked encoding being used.
The Workers implementation of theResponse interface includes several extensions to the web standardResponse API. These differences are intentional and provide additional functionality specific to the Workers runtime.
Workers type definitions (from@cloudflare/workers-types or generated viawrangler types) define aResponse type that includes Workers-specific properties likecf andwebSocket. This type is not directly compatible with the standardResponse type fromlib.dom.d.ts. If you are working with code that uses both Workers types and standard web types, you may need to use type assertions.
Workers adds an optionalcf property to theResponse object. This property can be set in theResponseInit options and is used for informational purposes by consumers of the Response. It does not affect Workers behavior.
Workers adds awebSocket property to theResponse object to support WebSocket connections. This property is present in successful WebSocket handshake responses. Refer toWebSockets for more information.
Workers adds anencodeBody option inResponseInit that controls how the response body is compressed. Set this to"manual" when serving pre-compressed data to prevent automatic compression.
Theheaders property returns a Workers-specificHeaders object that includes additional methods likegetAll() forSet-Cookie headers. Refer to theHeaders documentation for details on how the WorkersHeaders implementation differs from the web standard.
- Examples: Modify response
- Examples: Conditional response
- Reference:
Request - Write your Worker code inES modules syntax for an optimized experience.