Response
The Response
interface represents an HTTP response and is part of the Fetch API.
Constructor
let response = new Response(body, init);
Parameters
body
An object that defines the body text for the response. Can be
null
or any one of the following types:
init
- An
options
object that contains custom settings to apply to the response.
- An
Valid options for the options
object include:
status
int
- The status code for the response, such as
200
.
- The status code for the response, such as
statusText
string
- The status message associated with the status code, such as,
OK
.
- The status message associated with the status code, such as,
headers
Headers
|ByteString
- Any headers to add to your response that are contained within a
Headers
object or object literal ofByteString
key-value pairs.
- Any headers to add to your response that are contained within a
Properties
response.body
Readable Stream
- A getter to get the body contents.
response.bodyUsed
boolean
- A boolean indicating if the body was used in the response.
response.encodeBody
string
- Workers have to compress data according to the
content-encoding
header 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
response.headers
Headers
- The headers for the response.
response.ok
boolean
- 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.redirected
boolean
- A boolean indicating if the response is the result of a redirect. If so, its URL list has more than one entry.
response.status
int
- The status code of the response (for example,
200
to indicate success).
- The status code of the response (for example,
response.statusText
string
- The status message corresponding to the status code (for example,
OK
for200
).
- The status message corresponding to the status code (for example,
response.url
string
- The URL of the response. The value is the final URL obtained after any redirects.
response.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
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
Response
stream, reads it to completion, and returns a promise that resolves with anArrayBuffer
.
- Takes a
formData()
:Promise<FormData>
json()
:Promise<JSON>
text()
:Promise<USVString>
Set the Content-Length
header
The Content-Length
header will be automatically set by the runtime based on whatever the data source for the Response
is. Any value manually set by user code in the Headers
will be ignored. To have a Content-Length
header with a specific value specified, the body
of the Response
must be either a FixedLengthStream
or a fixed-length value just as a string or TypedArray
.
A FixedLengthStream
is an identity TransformStream
that permits only a fixed number of bytes to be written to it.
const { writable, readable } = new FixedLengthStream(11);
const enc = new TextEncoder(); const writer = writable.getWriter(); writer.write(enc.encode("hello world")); writer.end();
return new Response(readable);
Using any other type of ReadableStream
as the body of a response will result in chunked encoding being used.
Related resources
- Examples: Modify response
- Examples: Conditional response
- Reference:
Request
- Write your Worker code in ES modules syntax for an optimized experience.