files/en-us/web/api/requestinit/index.md
{{APIRef("Fetch API")}}
The RequestInit dictionary of the Fetch API represents the set of options that can be used to configure a fetch request.
You can pass a RequestInit object into the {{domxref("Request.Request()", "Request()")}} constructor, or directly into the fetch() function call.
You can also construct a Request with a RequestInit, and pass the Request to a fetch() call along with another RequestInit. If you do this, and the same option is set in both places, then the value passed directly into fetch() is used.
attributionReporting {{optional_inline}} {{deprecated_inline}}
: Indicates that you want the request's response to be able to register a JavaScript-based attribution source or attribution trigger. attributionReporting is an object containing the following properties:
eventSourceEligible
true, the request's response is eligible to register an attribution source. If set to false, it isn't.triggerEligible
true, the request's response is eligible to register an attribution trigger. If set to false, it isn't.See the Attribution Reporting API for more details.
body {{optional_inline}}
: The request body contains content to send to the server, for example in a {{httpmethod("POST")}} or {{httpmethod("PUT")}} request. It is specified as an instance of any of the following types:
See Setting a body for more details.
browsingTopics {{optional_inline}} {{deprecated_inline}}
: A boolean specifying that the selected topics for the current user should be sent in a {{httpheader("Sec-Browsing-Topics")}} header with the associated request.
See Using the Topics API for more details.
cache {{optional_inline}}
: The cache mode you want to use for the request. This may be any one of the following values:
default
no-store
reload
no-cache
force-cache
only-if-cached
The "only-if-cached" mode can only be used if the request's mode is "same-origin". Cached redirects will be followed if the request's redirect property is "follow" and the redirects do not violate the "same-origin" mode.
credentials {{optional_inline}}
: Controls whether or not the browser sends credentials with the request, as well as whether any Set-Cookie response headers are respected. Credentials are cookies, {{glossary("TLS")}} client certificates, or authentication headers containing a username and password. This option may be any one of the following values:
omit
same-origin
include
Including credentials in cross-origin requests can make a site vulnerable to {{glossary("CSRF")}} attacks, so even if credentials is set to include, the server must also agree to their inclusion by including the {{httpheader("Access-Control-Allow-Credentials")}} in its response. Additionally, in this situation the server must explicitly specify the client's origin in the {{httpheader("Access-Control-Allow-Origin")}} response header (that is, * is not allowed).
See Including credentials for more details.
Defaults to same-origin.
duplex {{optional_inline}} {{experimental_inline}}
: Controls duplex behavior of the request. If this is present it must have the value half, meaning that the browser must send the entire request before processing the response.
This option must be present when body is a {{domxref("ReadableStream")}}.
headers {{optional_inline}}
: Any headers you want to add to your request, contained within a {{domxref("Headers")}} object or an object literal whose keys are the names of headers and whose values are the header values.
Many headers are set automatically by the browser and can't be set by a script: these are called {{glossary("Forbidden request header", "Forbidden request headers")}}.
If the mode option is set to no-cors, you can only set {{glossary("CORS-safelisted request header", "CORS-safelisted request headers")}}.
See Setting headers for more details.
integrity {{optional_inline}}
: Contains the subresource integrity value of the request.
This will be checked when the resource is fetched, just as it would be when the integrity attribute is set on a {{htmlelement("script")}} element. The browser will compute the {{glossary("hash function", "hash")}} of the fetched resource using the specified algorithm, and if the result does not match the value specified, the browser will reject the fetch request with a network error.
The format of this option is <hash-algo>-<hash-source> where:
<hash-algo> is one of the following values: sha256, sha384, or sha512<hash-source> is the {{glossary("base64", "Base64-encoding")}} of the result of hashing the resource with the specified hash algorithm.Defaults to an empty string.
keepalive {{optional_inline}}
: A boolean.
When set to true, the browser will not abort the associated request if the page that initiated it is unloaded before the request is complete.
This enables a {{domxref('Window.fetch','fetch()')}} request to send analytics at the end of a session even if the user navigates away from or closes the page.
This has some advantages over using {{domxref("Navigator.sendBeacon()")}} for the same purpose.
For example, you can use HTTP methods other than POST, customize request properties, and access the server response via the fetch {{jsxref("Promise")}} fulfillment.
It is also available in service workers.
The body size for keepalive requests is limited to 64 kibibytes.
Defaults to false.
method {{optional_inline}}
: The request method.
Defaults to {{httpmethod("GET")}}.
mode {{optional_inline}}
: Sets cross-origin behavior for the request. One of the following values:
same-origin
same-origin request is sent to a different origin, the result is a network error.cors
no-cors
: Disables CORS for cross-origin requests. This option comes with the following restrictions:
HEAD, GET or POST.0.The main application for no-cors is for a service worker: although the response to a no-cors request can't be read by JavaScript, it can be cached by a service worker and then used as a response to an intercepted fetch request. Note that in this situation you don't know whether the request succeeded or not, so you should adopt a caching strategy which enables the cached response to be updated from the network (such as cache first with cache refresh).
navigate
navigate request is created only while navigating between documents.See Making cross-origin requests for more details.
Defaults to cors.
priority {{optional_inline}}
: Specifies the priority of the fetch request relative to other requests of the same type. Must be one of the following:
high
low
auto
Defaults to auto.
privateToken {{optional_inline}}
issuers
operation is set to send-redemption-record, in which case the issuers array must be included.operation
privateToken option, this property is mandatory. Possible values are:
token-request
token-redemption
send-redemption-record
refreshPolicy
operation is set to token-redemption. Possible values are:
none
refresh
version
1, which is the only version that the specification supports. When specifying the privateToken option, this property is mandatory.redirect {{optional_inline}}
: Determines the browser's behavior in case the server replies with a redirect status. One of the following values:
follow
error
manual
Defaults to follow.
referrer {{optional_inline}}
: A string specifying the value to use for the request's {{httpheader("Referer")}} header. One of the following:
Referer header to the given value. Relative URLs are resolved relative to the URL of the page that made the request.Referer header.about:client
Referer header to the default value for the context of the request (for example, the URL of the page that made the request).Defaults to about:client.
referrerPolicy {{optional_inline}}
signal {{optional_inline}}
AbortController.fetch()In this example we pass the method, body, and headers options directly into the fetch() method call:
async function post() {
const response = await fetch("https://example.org/post", {
method: "POST",
body: JSON.stringify({ username: "example" }),
headers: {
"Content-Type": "application/json",
},
});
console.log(response.status);
}
Request() constructorIn this example we create a {{domxref("Request")}}, passing the same set of options into its constructor, and then pass the request into fetch():
async function post() {
const request = new Request("https://example.org/post", {
method: "POST",
body: JSON.stringify({ username: "example" }),
headers: {
"Content-Type": "application/json",
},
});
const response = await fetch(request);
console.log(response.status);
}
Request() and fetch()In this example we create a {{domxref("Request")}}, passing the method, headers, and body options into its constructor. We then pass the request into fetch() along with body and referrer options:
async function post() {
const request = new Request("https://example.org/post", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username: "example1" }),
});
const response = await fetch(request, {
body: JSON.stringify({ username: "example2" }),
referrer: "",
});
console.log(response.status);
}
In this case the request will be sent with the following options:
method: "POST"headers: {"Content-Type": "application/json"}body: '{"username":"example2"}'referrer: ""{{Specifications}}