Skip to main content

HTTP Response

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

Constructor

The Response() constructor creates a new Response instance.

let response = new Response(body, init);

Parameters

nametypeoptionaldescription
bodyBlob, BufferSource, FormData, ReadableStream, URLSearchParams, or USVStringtrueThe body of the response. The default value is null.
initResponseInittrueAn optional object that allows setting status and headers of the response.

The return type is a Response instance.

ResponseInit
nametypeoptionaldescription
statusnumbertrueThe status code of the response.
statusTextstringtrueThe status message representative of the status code.
headersHeaders or string[][] or Record<string, string>falseThe HTTP headers of the response.

Properties

nametyperead onlydescription
bodyReadableStreamtrueThe getter exposes a ReadableStream of the body contents.
bodyUsedbooleantrueIndicates whether the body content is read.
urlUSVStringtrueThe URL of the response.
headersHeaderstrueThe headers associated with the response.
okbooleantrueIndicates if the response is successful (200-299 status).
redirectedbooleantrueIndicates if the response is the result of a redirect.
statusnumbertrueThe status code of the response
statusTextstringtrueThe status message of the response
typestringtrueThe type of the response.

Methods

namedescription
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

import { serve } from "https://deno.land/std@0.140.0/http/server.ts";

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;
}

serve(handler);