Request
{{APIRef("Fetch API")}}{{AvailableInWorkers}}
The Request interface of the Fetch API represents a resource request.
You can create a new Request object using the {{domxref("Request.Request","Request()")}} constructor, but you are more likely to encounter a Request object being returned as the result of another API operation, such as a service worker {{domxref("FetchEvent.request")}}request.
Constructor
{{domxref("Request.Request","Request()")}}- : Creates a new
Requestobject.
- : Creates a new
Instance properties
{{domxref("Request.body")}} {{ReadOnlyInline}}body- : A
{{domxref("ReadableStream")}}ReadableStream of the body contents.
- : A
{{domxref("Request.bodyUsed")}} {{ReadOnlyInline}}bodyUsed- : Stores
trueorfalseto indicate whether or not the body has been used in a request yet.
- : Stores
{{domxref("Request.cache")}} {{ReadOnlyInline}}cache- : Contains the cache mode of the request (e.g.,
default,reload,no-cache).
- : Contains the cache mode of the request (e.g.,
{{domxref("Request.credentials")}} {{ReadOnlyInline}}credentials- : Contains a value controlling whether credentials should be included with the request (e.g.,
omit,same-origin,include). The default issame-origin.
- : Contains a value controlling whether credentials should be included with the request (e.g.,
{{domxref("Request.destination")}} {{ReadOnlyInline}}destination- : A string describing the type of content being requested.
{{domxref("Request.duplex")}} {{ReadOnlyInline}} {{experimental_inline}}duplex- : The duplex mode of the request, which determines whether the browser must send the entire request before processing the response.
{{domxref("Request.headers")}} {{ReadOnlyInline}}headers- : Contains the associated
{{domxref("Headers")}}Headers object of the request.
- : Contains the associated
{{domxref("Request.integrity")}} {{ReadOnlyInline}}integrity- : Contains the subresource integrity value of the request (e.g.,
sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
- : Contains the subresource integrity value of the request (e.g.,
{{domxref("Request.isHistoryNavigation")}} {{ReadOnlyInline}}isHistoryNavigation- : A boolean indicating whether the request is a history navigation.
{{domxref("Request.isReloadNavigation")}} {{ReadOnlyInline}} {{experimental_inline}}isReloadNavigation- : A boolean indicating whether the request is a user-triggered reload.
{{domxref("Request.keepalive")}} {{ReadOnlyInline}}keepalive- : Contains the request's
keepalivesetting (trueorfalse), which indicates whether the browser will keep the associated request alive if the page that initiated it is unloaded before the request is complete.
- : Contains the request's
{{domxref("Request.method")}} {{ReadOnlyInline}}method- : Contains the request's method (
GET,POST, etc.)
- : Contains the request's method (
{{domxref("Request.mode")}} {{ReadOnlyInline}}mode- : Contains the mode of the request (e.g.,
cors,no-cors,same-origin,navigate.)
- : Contains the mode of the request (e.g.,
{{domxref("Request.redirect")}} {{ReadOnlyInline}}redirect- : Contains the mode for how redirects are handled. It may be one of
follow,error, ormanual.
- : Contains the mode for how redirects are handled. It may be one of
{{domxref("Request.referrer")}} {{ReadOnlyInline}}referrer- : Contains the referrer of the request (e.g.,
client).
- : Contains the referrer of the request (e.g.,
{{domxref("Request.referrerPolicy")}} {{ReadOnlyInline}}referrerPolicy- : Contains the referrer policy of the request (e.g.,
no-referrer).
- : Contains the referrer policy of the request (e.g.,
{{domxref("Request.signal")}} {{ReadOnlyInline}}signal- : Returns the
{{domxref("AbortSignal")}}AbortSignal associated with the request.
- : Returns the
{{domxref("Request.targetAddressSpace")}} {{ReadOnlyInline}} {{experimental_inline}}targetAddressSpace- : Returns the request's target address space, which indicates whether it is a loopback, local, or public request.
{{domxref("Request.url")}} {{ReadOnlyInline}}url- : Contains the URL of the request.
Instance methods
{{domxref("Request.arrayBuffer()")}}- : Returns a promise that resolves with an
{{jsxref("ArrayBuffer")}}ArrayBuffer representation of the request body.
- : Returns a promise that resolves with an
{{domxref("Request.blob()")}}- : Returns a promise that resolves with a
{{domxref("Blob")}}Blob representation of the request body.
- : Returns a promise that resolves with a
{{domxref("Request.bytes()")}}- : Returns a promise that resolves with a
{{jsxref("Uint8Array")}}Uint8Array representation of the request body.
- : Returns a promise that resolves with a
{{domxref("Request.clone()")}}- : Creates a copy of the current
Requestobject.
- : Creates a copy of the current
{{domxref("Request.formData()")}}- : Returns a promise that resolves with a
{{domxref("FormData")}}FormData representation of the request body.
- : Returns a promise that resolves with a
{{domxref("Request.json()")}}- : Returns a promise that resolves with the result of parsing the request body as
{{JSxRef("JSON")}}JSON.
- : Returns a promise that resolves with the result of parsing the request body as
{{domxref("Request.text()")}}- : Returns a promise that resolves with a text representation of the request body.
{{domxref("Request.textStream()")}} {{experimental_inline}}- : Returns a
{{domxref("ReadableStream")}}ReadableStream that can be used to read the contents of the request body in chunks of UTF-8.
- : Returns a
[!NOTE]NOTE: The request body functions can be run only once; subsequent calls will reject with TypeError showing that the body stream has already used.
Examples
In the following snippet, we create a new request using the Request() constructor (for an image file in the same directory as the script), then return some property values of the request:
const request = new Request("https://www.mozilla.org/favicon.ico");
const url = request.url;
const method = request.method;
const credentials = request.credentials;
You could then fetch this request by passing the Request object in as a parameter to a {{domxref("Window/fetch", "fetch()")}} call, for example:
fetch(request)
.then((response) => response.blob())
.then((blob) => {
image.src = URL.createObjectURL(blob);
});
In the following snippet, we create a new request using the Request() constructor with some initial data and body content for an API request which needs a body payload:
const request = new Request("https://example.com", {
method: "POST",
body: '{"foo": "bar"}',
});
const url = request.url;
const method = request.method;
const credentials = request.credentials;
const bodyUsed = request.bodyUsed;
[!NOTE]NOTE: The body can only be a{{domxref("Blob")}}Blob, an{{jsxref("ArrayBuffer")}}ArrayBuffer, a{{jsxref("TypedArray")}}TypedArray, a{{jsxref("DataView")}}DataView, a{{domxref("FormData")}}FormData, a{{domxref("URLSearchParams")}}URLSearchParams, a{{domxref("ReadableStream")}}ReadableStream, or a{{jsxref("String")}}String object, as well as a string literal, so for adding a JSON object to the payload you need to stringify that object.
You could then fetch this API request by passing the Request object in as a parameter to a {{domxref("Window/fetch", "fetch()")}} call, for example and get the response:
fetch(request)
.then((response) => {
if (response.status !== 200) {
throw new Error("Something went wrong on API server!");
}
return response.json();
})
.then((response) => {
console.debug(response);
// …
})
.catch((error) => {
console.error(error);
});
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}