Response

Last update:2024-08-14 19:50:32

The Response interface represents an HTTP response and is part of the Fetch API. It contains information about the response, such as the status code, status message, headers, and body. You can use the Response object to construct custom HTTP responses and return them to clients within your Edge Cloud Apps Functions.

Constructor

const response = new Response(body?, init?);

Parameters

  • body (Optional):

    The body content of the Response object. It can be null or one of the following types:

    • string
    • ArrayBuffer
    • Blob
    • ReadableStream
    • FormData
  • init (Optional):

    An options object containing custom settings to apply to the response.
    Valid options include:

    • status: The status code for the response, such as 200.
    • statusText: The status message associated with the status code, such as OK.
    • headers: Any headers to add to your response contained within a Headers object or an object literal of ByteString key-value pairs.
    • encodeBody: Indicates how to compress the response body. The default value is "automatic", which means that Workers will automatically compress the data according to the Content-Encoding header. To serve data that is already compressed, set this property to "manual".
    • 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.

Instance Properties

body

// response.body
readonly body: ReadableStream;

The response body, represented as a ReadableStream object. For more information, see [ReadableStream](link to ReadableStream API documentation).

bodyUsed

// response.bodyUsed
readonly bodyUsed: boolean;

A boolean value indicating whether the response body has been read.

headers

// response.headers
readonly headers: Headers;

The response headers, represented as a Headers object. For more information, see [Headers](link to Headers API documentation).

ok

// response.ok
readonly ok: boolean;

A boolean value indicating whether the response was successful (status code in the range 200-299).

status

// response.status
readonly status: number;

The response status code.

statusText

// response.statusText
readonly statusText: string;

The response status message.

url

// response.url
readonly url: string;

The URL of the response. The value is the final URL obtained after any redirects.

redirected

// response.redirected
readonly redirected: boolean;

A boolean value indicating whether the response is the result of a redirect.

redirectUrls

// response.redirectUrls
readonly redirectUrls: Array<String>;

An array of all redirect URLs.

type

// response.type
readonly type: string;

The type of the response. Its value can be “basic”, “cors”, “error”, or “opaque”.

webSocket

// response.webSocket
readonly webSocket: 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.

Instance Methods

Instance methods

  • clone(): Creates a clone of a Response object.
  • json(): Creates a new response with a JSON-serialized payload.
  • redirect(): 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(): Takes a Response stream, reads it to completion, and returns a promise that resolves with an ArrayBuffer.
  • formData(): Takes a Response stream, reads it to completion, and returns a promise that resolves with a FormData object.
  • json(): Takes a Response stream, reads it to completion, and returns a promise that resolves with the result of parsing the body text as JSON.
  • text(): Takes a Response stream, reads it to completion, and returns a promise that resolves with a USVString (text).

Setting 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.

References

Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!