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
name | type | optional | description |
---|---|---|---|
resource | Request or USVString | false | The resource can either be a request object or a URL string. |
init | RequestInit | true | The init object lets you set optional parameters to apply to the request. |
The return type is a Request
instance.
RequestInit
name | type | default | description |
---|---|---|---|
method | string | GET | The method of the request. |
headers | Headers or { [key: string]: string } | none | Th Headers for the request. |
body | Blob , BufferSource , FormData , URLSearchParams , USVString , or ReadableStream | none | The body of the request. |
cache | string | none | The cache mode of the request. |
credentials | string | same-origin | The credentials mode of the request. |
integrity | string | none | The crypotographic hash of the request's body. |
mode | string | cors | The request mode you want to use. |
redirect | string | follow | The mode of how redirects are handled. |
referrer | string | about:client | A USVString specifying no-referrer , client or a URL. |
Properties
name | type | description |
---|---|---|
cache | string | The cache mode indicates how the (default , no-cache , etc) request should be cached by browser. |
credentials | string | The credentials (omit , same-origin , etc) indicate whether user agent should send cookies in case of CORs of the request. |
destination | RequestDestination | The string indicates the type of content being requested. |
body | ReadableStream | The getter exposes a ReadableStream of the body contents. |
bodyUsed | boolean | Indicates whether the body content is read. |
url | USVString | The URL of the request. |
headers | Headers | The headers associated with the request. |
integrity | string | The crypotographic hash of the request's body. |
method | string | The request's method (POST , GET , etc). |
mode | string | Indicates the mode of the request (e.g. cors ). |
redirect | string | The mode of how redirects are handled. |
referrer | string | The referrer of the request. |
referrerPolicy | string | The referrer policy of the request |
All the above properties are read only.
Methods
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 Request object. |
Example
import { serve } from "https://deno.land/std@0.202.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);