Skip to main content
On this page

HTTP Response

The Response interface is part of the Fetch API and represents a response resource of fetch().

Constructor Jump to heading

The Response() constructor creates a new Response instance.

let response = new Response(body, init);

Parameters Jump to heading

name type optional description
body Blob, BufferSource, FormData, ReadableStream, URLSearchParams, or USVString true The body of the response. The default value is null.
init ResponseInit true An optional object that allows setting status and headers of the response.

The return type is a Response instance.

ResponseInit Jump to heading
name type optional description
status number true The status code of the response.
statusText string true The status message representative of the status code.
headers Headers or string[][] or Record<string, string> false The HTTP headers of the response.

Properties Jump to heading

name type read only description
body ReadableStream true The getter exposes a ReadableStream of the body contents.
bodyUsed boolean true Indicates whether the body content is read.
url USVString true The URL of the response.
headers Headers true The headers associated with the response.
ok boolean true Indicates if the response is successful (200-299 status).
redirected boolean true Indicates if the response is the result of a redirect.
status number true The status code of the response
statusText string true The status message of the response
type string true The type of the response.

Methods Jump to heading

name description
arrayBuffer() Reads the body stream to its completion and returns an ArrayBuffer object.
blob() Reads the body stream to its completion and returns a Blob object.
formData() Reads the body stream to its completion and returns a FormData object.
json() Reads the body stream to its completion, parses it as JSON and returns a JavaScript object.
text() Reads the body stream to its completion and returns a USVString object (text).
clone() Clones the response object.
error() Returns a new response object associated with a network error.
redirect(url: string, status?: number) Creates a new response that redirects to the provided URL.

Example Jump to heading

function handler(_req) {
  // Create a response with html as its body.
  const response = new Response("<html> Hello </html>", {
    status: 200,
    headers: {
      "content-type": "text/html",
    },
  });

  console.log(response.status); // 200
  console.log(response.headers.get("content-type")); // text/html

  return response;
}

Deno.serve(handler);