Skip to main content

HTTP Request

The Request interface is part of the Fetch API and represents the request of fetch().

Constructor

The Request() constructor creates a new Request instance.

let request = new Request(input, init);

Parameters

nametypeoptionaldescription
resourceRequest or USVStringfalseThe resource can either be a request object or a URL string.
initRequestInittrueThe init object lets you set optional parameters to apply to the request.

The return type is a Request instance.

RequestInit
nametypedefaultdescription
methodstringGETThe method of the request.
headersHeaders or { [key: string]: string }noneTh Headers for the request.
bodyBlob, BufferSource, FormData, URLSearchParams, USVString, or ReadableStreamnoneThe body of the request.
cachestringnoneThe cache mode of the request.
credentialsstringsame-originThe credentials mode of the request.
integritystringnoneThe crypotographic hash of the request's body.
modestringcorsThe request mode you want to use.
redirectstringfollowThe mode of how redirects are handled.
referrerstringabout:clientA USVString specifying no-referrer, client or a URL.

Properties

nametypedescription
cachestringThe cache mode indicates how the (default, no-cache, etc) request should be cached by browser.
credentialsstringThe credentials (omit, same-origin, etc) indicate whether user agent should send cookies in case of CORs of the request.
destinationRequestDestinationThe string indicates the type of content being requested.
bodyReadableStreamThe getter exposes a ReadableStream of the body contents.
bodyUsedbooleanIndicates whether the body content is read.
urlUSVStringThe URL of the request.
headersHeadersThe headers associated with the request.
integritystringThe crypotographic hash of the request's body.
methodstringThe request's method (POST, GET, etc).
modestringIndicates the mode of the request (e.g. cors ).
redirectstringThe mode of how redirects are handled.
referrerstringThe referrer of the request.
referrerPolicystringThe referrer policy of the request

All the above properties are read only.

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 Request object.

Example

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

function handler(_req) {
// Create a post request
const request = new Request("https://post.deno.dev", {
method: "POST",
body: JSON.stringify({
message: "Hello world!",
}),
headers: {
"content-type": "application/json",
},
});

console.log(request.method); // POST
console.log(request.headers.get("content-type")); // application/json

return fetch(request);
}

serve(handler);