Response

The Response Web API allows you to construct a Response object to be returned in your webhook handler, but also represents the received response for your fetch requests and gives you access to the response’s headers, body and other properties.

Signature

declare class Response extends Body {
  constructor(bodyInit?: BodyInit | null, maybeInit?: ResponseInit | Response);
  static redirect(url: string, status?: number): Response;
  static json(any: any, maybeInit?: ResponseInit | Response): Response;
  clone(): Response;
  readonly status: number;
  readonly statusText: string;
  readonly headers: Headers;
  readonly ok: boolean;
  readonly redirected: boolean;
  readonly url: string;
  readonly webSocket: WebSocket | null;
  readonly cf?: Object;
}

Constructor

  • Response(): Response
    • The Response() constructor creates a new Response object.

Properties

  • body: ReadableStream
    • A ReadableStream representing the request’s body. This can be accessed via helper methods such as text() and json().

  • bodyUsed: boolean
    • A boolean used to tell if the body has been consumed or not.

      Example Usage
      Example: bodyUsed
      export default {
      	async webhook(req: Request, env: Repeat.Env) {
      		const res = await fetch("https://google.com");
      		console.log(res.bodyUsed); // false
      		const body = await res.text();
      		console.log(res.bodyUsed); // true
      		
      		const body2 = await res.text(); // Exception: body has already been used.
      	}
      }
  • status: Number
    • Represents the HTTP status code of the response.

  • statusText: string
    • Represents the text associated with a HTTP status code, such as NOT FOUND for a 404 status code.

  • headers: Headers
    • A Headers object used by the the response. Headers cannot be accessed via indexing, such as headers["content-type"], and instead expose methods such as get() or set().

      Example Usage
      export default {
      	async webhook(request: Request) {
      		const response = await fetch("https://google.com");
      		const contentType = response.headers.get("Content-Type");
      
      		if (contentType) {
      			console.log(`Response's Content-Type is: ${contentType}`);
      		}
      	},
      };
  • ok: boolean
    • A boolean representing if the request was successful.

  • redirected: boolean
    • Represents if the response is the result of a redirect.

  • url: string
    • The final URL of where the response was received from.

Methods

  • clone() → Response
    • Creates a cloned instance of the Response object.

  • redirect() → Response
    • Return a redirect to the provided URL.

  • json() → Response
    • Return a JSON response which is the result of calling JSON.stringify on the provided object. The JSON is not pretty-printed.

Examples

Return a response from your handler

Example #1
export default {
	async webhook(request: Request, env: Repeat.Env) {
		return new Response("Hello world!")
	},
};
 

Last updated on November 20, 2022