The Request interface of the Fetch API represents a resource request.
You can create a new Request object using the 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 FetchEvent.request.
Request.Request()Request object.Request.cache Read only
default, reload, no-cache).Request.context Read only
audio, image, iframe, etc.)Request.credentials Read only
"omit", "same-origin", "include"). The default is "same-origin".Request.destination Read only
RequestDestination enum describing the request's destination. This is a string indicating the type of content being requested.Request.headers Read only
Headers object of the request.Request.integrity Read only
sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).Request.method Read only
GET, POST, etc.)Request.mode Read only
cors, no-cors, same-origin, navigate.)Request.redirect Read only
follow, error, or manual.Request.referrer Read only
client).Request.referrerPolicy Read only
no-referrer).Request.url Read only
Request implements Body, so it also inherits the following properties:
body Read only
ReadableStream of the body contents.bodyUsed Read only
Boolean that declares whether the body has been used in a response yet.Request.clone()Request object.Request implements Body, so it also has the following methods available to it:
Body.arrayBuffer()ArrayBuffer representation of the request body.Body.blob()Blob representation of the request body.Body.formData()FormData representation of the request body.Body.json()JSON representation of the request body.Body.text()USVString (text) representation of the request body.Note: The Body functions can be run only once; subsequent calls will resolve with empty strings/ArrayBuffers.
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 myRequest = new Request('http://localhost/flowers.jpg');
const myURL = myRequest.url; // http://localhost/flowers.jpg
const myMethod = myRequest.method; // GET
const myCred = myRequest.credentials; // same-origin
You could then fetch this request by passing the Request object in as a parameter to a WindowOrWorkerGlobalScope.fetch() call, for example:
fetch(myRequest)
.then(response => response.blob())
.then(blob => {
myImage.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 need a body payload:
const myRequest = new Request('http://localhost/api', {method: 'POST', body: '{"foo":"bar"}'});
const myURL = myRequest.url; // http://localhost/api
const myMethod = myRequest.method; // POST
const myCred = myRequest.credentials; // same-origin
const bodyUsed = myRequest.bodyUsed; // true
Note: The body type can only be a Blob, BufferSource, FormData, URLSearchParams, USVString or ReadableStream type, 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 WindowOrWorkerGlobalScope.fetch() call, for example and get the response:
fetch(myRequest)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error('Something went wrong on api server!');
}
})
.then(response => {
console.debug(response);
// ...
}).catch(error => {
console.error(error);
}); | Specification | Status | Comment |
|---|---|---|
| Fetch The definition of 'Request' in that specification. | Living Standard | Initial definition |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 42 | Yes | 39 | No | 28 | No |
Request() constructor
|
42
|
15 | 39
|
No | 29
|
10.1 |
cache
|
No | Yes | 48 | No | No | 11 |
clone
|
42
|
Yes | 39
|
No | 29
|
No |
context
|
42 — 46 | ? | 39 — 42
|
No | 28 — 29 | No |
credentials
|
42
|
Yes | 39
|
No | 29
|
No |
destination
|
65 | 14 | 61 | No | 52 | No |
headers
|
42
|
Yes | 39
|
No | 29
|
No |
integrity
|
46 | Yes | Yes | No | Yes | No |
keepalive
|
66 | 15 | ? | ? | 43 | ? |
method
|
42
|
Yes | 39
|
No | 29
|
No |
mode
|
42 | Yes | 39 | No | 29 | No |
redirect
|
46 | Yes | Yes | No | Yes | No |
referrer
|
42
|
Yes | 47 | No | 29
|
No |
referrerPolicy |
52 | No | 52 | No | 39 | 11.1 |
signal |
? | 16 | ? | ? | ? | ? |
url
|
42
|
Yes | 39
|
No | 29
|
No |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | 42 | 42 | Yes | Yes | 28 | No | 4.0 |
Request() constructor
|
42
|
42
|
Yes | Yes | No | 10.3 | 4.0
|
cache
|
No | No | Yes | No | No | No | No |
clone
|
No | No | Yes | No | No | No | No |
context
|
42 — 46 | 42 — 46 | ? | No | No | No | 4.0 |
credentials
|
No | 42 — 46 | Yes | No | No | No | 4.0 |
destination
|
? | 65 | ? | 61 | 52 | No | ? |
headers
|
No | 42 — 46 | Yes | No | No | No | 4.0 |
integrity
|
No | 46 | Yes | No | Yes | No | 5.0 |
keepalive
|
No | 66 | ? | ? | 43 | ? | No |
method
|
No | 42 — 46 | Yes | No | No | No | 4.0 |
mode
|
49 | 49 | Yes | No | No | No | 5.0 |
redirect
|
No | 46 | Yes | Yes | Yes | No | 5.0 |
referrer
|
No | No | Yes | No | No | No | No |
referrerPolicy |
52 | 52 | No | 52 | 39 | No | 7.2 |
signal |
? | ? | ? | ? | ? | ? | ? |
url
|
42
|
42
|
Yes | No | 29
|
No | 4.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Request