Response
Constructor
let response = new Response(body, init);
Parameters
bodyoptional An 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 the options object include:
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, like,
OK.
- The status message associated with the status code, like,
headersHeaders |ByteString - Any headers to add to your response that are contained within a
Headersobject or object literal ofByteStringkey/value pairs.
- Any headers to add to your response that are contained within a
Properties
bodyReadable Stream - A simple getter to get the body contents.
bodyUsedboolean - A boolean indicating if the body was used in the response.
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"auto".
- Workers have to compress data according to the
headersHeaders - The headers for the response.
okboolean - A boolean indicating if the response was successful (status in the range 200-299).
redirectedboolean - A boolean indicating if the response is the result of a redirect. If so, its URL list has more than one entry.
statusint - The status code of the response (for example,
200to indicate success).
- The status code of the response (for example,
statusTextstring - The status message corresponding to the status code (for example,
OKfor200).
- The status message corresponding to the status code (for example,
urlstring - The URL of the response. The value is the final URL obtained after any redirects.
webSocket- 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
Methods
Instance methods
clone()Response - Creates a clone of a
Responseobject.
- Creates a clone of a
redirect()Response - Creates a new response with a different URL.
Additional instance methods
Response implements the Body mixin of the Fetch API, and therefore Response 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>}{’ ‘}json()Promise{ <JSON>}{’ ‘}text()Promise{ <USVString>}{’ ‘}