INTEGRITY Cloudflare Docs

Response

The Response interface represents an HTTP response and is part of the Fetch API.


Constructor

let response = new Response(body, init);

Parameters

Valid options for the options object include:

Properties

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:

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.


Differences

The Workers implementation of the Response interface includes several extensions to the web standard Response API. These differences are intentional and provide additional functionality specific to the Workers runtime.

The cf property

Workers adds an optional cf property to the Response object. This property can be set in the ResponseInit options and is used for informational purposes by consumers of the Response. It does not affect Workers behavior.

The webSocket property

Workers adds a webSocket property to the Response object to support WebSocket connections. This property is present in successful WebSocket handshake responses. Refer to WebSockets for more information.

The encodeBody option

Workers adds an encodeBody option in ResponseInit that controls how the response body is compressed. Set this to "manual" when serving pre-compressed data to prevent automatic compression.

The headers property

The headers property returns a Workers-specific Headers object that includes additional methods like getAll() for Set-Cookie headers. Refer to the Headers documentation for details on how the Workers Headers implementation differs from the web standard.