Fetch RequestInit - Scoped, Global
The RequestInit API provides options to configure a Fetch request.
You can pass a RequestInit object into the Request() constructor, or directly into the fetch() function call. You can also construct a Request with a RequestInit() call, and pass the Request to a fetch() call along with another RequestInit(). If you do this when the same option is set in both places, then the value passed directly into fetch() is used.
- Fetch - fetch(String resource, Object options): Starts the process of fetching a resource from the network.
- Fetch Headers - Scoped, Global: Retrieve and modify request and response headers.
- Fetch Request - Scoped, Global: Create a new request object.
- Fetch RequestInit - Scoped, Global: Set options to configure a fetch request.
- Fetch Response - Scoped,Global: Create a new response object.
To support fetch actions, the system property, glide.hosts.allowlist, allows controls over what hosts a fetch method can access. It applies to HTTP APIs like RestMessageV2 and those mentioned above. For more
information about glide.hosts.allowlist, see Available system properties.
Fetch RequestInit - RequestInit
Set options to configure a Fetch request. You can pass a RequestInit object into the Request() constructor or directly into the fetch() function call.
RequestInit properties
The RequestInit API supports several optional properties that can be used to configure HTTP request behavior. Its properties include method (HTTP method), headers (request
headers), body (data to send), mode (CORS mode), credentials (authentication details), cache (cache behavior), and others like redirect,
referrer, and integrity. These options let you fine-tune request behavior for various use cases. To read more in-depth explanations about each property, see https://developer.mozilla.org/en-US/docs/Web/API/RequestInit.
| Property name | Description |
|---|---|
| attributionReporting | Optional. 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:
Data type: Object |
| body | Optional. The request body contains content to send to the server, for example in a POST or PUT request. Accepted types:
|
| browsingTopics | Optional. Flag indicating whether the selected topics for the current user should be sent in a Sec-Browsing-Topics header with the associated request. Accepted values:
Data type: Boolean |
| cache | Optional. The cache mode to use for the request. Accepted values:
The Data type: String |
| credentials | Optional. 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, TLS client certificates, or authentication
headers containing a username and password. Accepted values:
Default: same-origin Data type: String |
| headers | Optional. Any headers to add to your request, contained within a 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 Forbidden header names. If the Data type: String |
| integrity | Optional. Contains the subresource integrity value of the request. This is checked when the resource is fetched. The browser computes the hash of the fetched resource using the specified algorithm, and if the result
does not match the value specified, the browser rejects the fetch request with a network error. The format of this option is <hash-algo>-<hash-source> where:
Defaults to an empty string. Data type: String |
| keepalive | Optional. Flag that indicates whether to abort the associated request if the page that initiated it is unloaded before the request is complete. The body size for Valid values:
Data type: Boolean Default: false |
| method | Optional. The request method. Data type: String Default: GET |
| mode | Optional. Sets cross-origin behavior for the request. Valid values:
Data type: String Default: cors |
| priority | Optional. Specifies the priority of the fetch request relative to other requests of the same type. Valid values:
Data type: String Default: auto |
| redirect | Optional. Determines the browser's behavior in case the server replies with a redirect status. Accepted values:
Data type: String Default: follow |
| referrer | Optional. A string specifying the value to use for the request's Referrer header. Accepted values and data types:
Default: about:client |
| referrerPolicy | Optional. A string that sets a policy for the Referrer header. The syntax and semantics of this option are exactly the same as for the Referrer-Policy header. Data type: String |
| signal | Optional. An AbortSignal. If this option is set, the request can be canceled by calling abort() on the corresponding AbortController.Data type: String |
| Type | Description |
|---|---|
| None |
Pass options into fetch()
This example shows how to pass the method, body, and headers options directly into the fetch() method:
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);
}
Pass options into the Request() constructor
This example shows how to create a Request and pass the method, body, and headers options into the constructor and 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);
}
Pass options into both Request() and fetch()
This example shows ho to create a Request() and pass the method, body, and headers options into the constructor. The script then passes the request into fetch() along with the body and referrer options.
- method: "POST"
- headers: {"Content-Type": "application/json"}
- body: '{"username":"example2"}'
- referrer: ""
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);
}