Request

Cette traduction est incomplète. Aidez-nous à traduire cet article depuis l’anglais

The Request interface of the Fetch API represents a resource request.

You can create a new Request object using the 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.

Constructor

Request()
Creates a new Request object.

Properties

Request.cache Lecture seule
Contains the cache mode of the request (e.g., default, reload, no-cache).
Request.context Lecture seule
Contains the context of the request (e.g., audio, image, iframe, etc.)
Request.credentials Lecture seule
Contains the credentials of the request (e.g., omit, same-origin, include). The default is same-origin.
Request.destination Lecture seule
Returns a string from the RequestDestination enum describing the request's destination. This is a string indicating the type of content being requested.
Request.headers Lecture seule
Contains the associated Headers object of the request.
Request.integrity Lecture seule
Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
Request.method Lecture seule
Contains the request's method (GET, POST, etc.)
Request.mode Lecture seule
Contains the mode of the request (e.g., cors, no-cors, same-origin, navigate.)
Request.redirect Lecture seule
Contains the mode for how redirects are handled. It may be one of follow, error, or manual.
Request.referrer Lecture seule
Contains the referrer of the request (e.g., client).
Request.referrerPolicy Lecture seule
Contains the referrer policy of the request (e.g., no-referrer).
Request.url Lecture seule
Contains the URL of the request.

Request implements Body, so it also inherits the following properties:

body Lecture seule
A simple getter used to expose a ReadableStream of the body contents.
bodyUsed Lecture seule
Stores a Boolean that declares whether the body has been used in a response yet.

Methods

Request.clone()
Creates a copy of the current Request object.

Request implements Body, so it also has the following methods available to it:

Body.arrayBuffer()
Returns a promise that resolves with an ArrayBuffer representation of the request body.
Body.blob()
Returns a promise that resolves with a Blob representation of the request body.
Body.formData()
Returns a promise that resolves with a FormData representation of the request body.
Body.json()
Returns a promise that resolves with a JSON representation of the request body.
Body.text()
Returns a promise that resolves with an USVString (text) representation of the request body.

Note: The Body functions can be run only once; subsequent calls will resolve with empty strings/ArrayBuffers.

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 WindowOrWorkerGlobalScope.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 need 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: 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(request)
  .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);
  });

Specifications

Specification Status Comment
Fetch
La définition de 'Request' dans cette spécification.
Standard évolutif Initial definition

Browser compatibility

Update compatibility data on GitHub
OrdinateurMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariWebview AndroidChrome pour AndroidFirefox pour AndroidOpera pour AndroidSafari sur iOSSamsung Internet
Request
Expérimentale
Chrome Support complet 42
Support complet 42
Support complet 41
Désactivée
Désactivée From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Support complet ≤18Firefox Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Aucun support NonOpera Support complet 28Safari Support complet 10.1WebView Android Support complet 42Chrome Android Support complet 42
Support complet 42
Support complet 41
Désactivée
Désactivée From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android Support complet 28Safari iOS Support complet 10.3Samsung Internet Android Support complet 4.0
Request() constructor
Expérimentale
Chrome Support complet 41
Notes
Support complet 41
Notes
Notes From Chrome 47, default values for the init argument's properties changed. mode defaults to same-origin (from no-cors). credentials defaults to include (from same-origin). redirect defaults to follow (from manual).
Edge Support complet 15Firefox Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Aucun support NonOpera Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Support complet 10.1WebView Android Support complet 42
Notes
Support complet 42
Notes
Notes From WebView 47, default values for the init argument's properties changed. mode defaults to same-origin (from no-cors). credentials defaults to include (from same-origin). redirect defaults to follow (from manual).
Chrome Android Support complet 41
Notes
Support complet 41
Notes
Notes From Chrome 47, default values for the init argument's properties changed. mode defaults to same-origin (from no-cors). credentials defaults to include (from same-origin). redirect defaults to follow (from manual).
Firefox Android Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS Support complet 10.3Samsung Internet Android Support complet 4.0
Support complet 4.0
Support complet 5.0
Notes
Notes Some default values for the init parameter changed in Samsung Internet 5.0. See the Properties section for details.
cache
Expérimentale
Chrome Support complet 64Edge Support complet 14Firefox Support complet 48IE Aucun support NonOpera Support complet 51Safari Support complet 11WebView Android Support complet 64Chrome Android Support complet 64Firefox Android Aucun support NonOpera Android Support complet 47Safari iOS Aucun support NonSamsung Internet Android Support complet 9.0
clone
Expérimentale
Chrome Support complet 42
Support complet 42
Support complet 41
Désactivée
Désactivée From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Support complet 14Firefox Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Aucun support NonOpera Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Aucun support NonWebView Android Aucun support NonChrome Android Aucun support NonFirefox Android Aucun support NonOpera Android Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS Aucun support NonSamsung Internet Android Aucun support Non
context
ExpérimentaleObsolèteNon-standard
Chrome Aucun support 42 — 46Edge ? Firefox Aucun support 39 — 42
Notes
Aucun support 39 — 42
Notes
Notes see bug 1188062 for more information.
IE Aucun support NonOpera Aucun support 28 — 29Safari Aucun support NonWebView Android Aucun support 42 — 46Chrome Android Aucun support 42 — 46Firefox Android Aucun support NonOpera Android Aucun support NonSafari iOS Aucun support NonSamsung Internet Android Aucun support 4.0 — 5.0
credentials
Expérimentale
Chrome Support complet 42
Support complet 42
Support complet 41
Désactivée
Désactivée From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Support complet 14Firefox Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Aucun support NonOpera Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Aucun support NonWebView Android Support complet 42Chrome Android Support complet 42Firefox Android Aucun support NonOpera Android Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS Aucun support NonSamsung Internet Android Support complet 4.0
destination
Expérimentale
Chrome Support complet 65Edge Support complet 14Firefox Support complet 61IE Aucun support NonOpera Support complet 52Safari Support complet 10.1WebView Android Support complet 65Chrome Android Support complet 65Firefox Android Support complet 61Opera Android Support complet 47Safari iOS Support complet 10.3Samsung Internet Android Support complet 9.0
headers
Expérimentale
Chrome Support complet 42
Support complet 42
Support complet 41
Désactivée
Désactivée From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Support complet 14Firefox Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Aucun support NonOpera Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Aucun support NonWebView Android Aucun support NonChrome Android Aucun support 42 — 46Firefox Android Aucun support NonOpera Android Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS Aucun support NonSamsung Internet Android Aucun support 4.0 — 5.0
integrity
Expérimentale
Chrome Support complet 46Edge Support complet 14Firefox Support complet OuiIE Aucun support NonOpera Support complet OuiSafari Aucun support NonWebView Android Aucun support NonChrome Android Support complet 46Firefox Android Aucun support NonOpera Android Support complet OuiSafari iOS Aucun support NonSamsung Internet Android Support complet 5.0
keepalive
Expérimentale
Chrome Support complet 66Edge Support complet 15Firefox ? IE Aucun support NonOpera Support complet 43Safari Aucun support NonWebView Android Support complet 66Chrome Android Support complet 66Firefox Android ? Opera Android Support complet 43Safari iOS Aucun support NonSamsung Internet Android Support complet 9.0
method
Expérimentale
Chrome Support complet 42
Support complet 42
Support complet 41
Désactivée
Désactivée From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Support complet 14Firefox Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Aucun support NonOpera Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Aucun support NonWebView Android Aucun support NonChrome Android Aucun support 42 — 46Firefox Android Aucun support NonOpera Android Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS Aucun support NonSamsung Internet Android Aucun support 4.0 — 5.0
mode
Expérimentale
Chrome Support complet 42Edge Support complet 14Firefox Support complet 39IE Aucun support NonOpera Support complet 29Safari Aucun support NonWebView Android Support complet 49Chrome Android Support complet 49Firefox Android Aucun support NonOpera Android Aucun support NonSafari iOS Aucun support NonSamsung Internet Android Support complet 5.0
redirect
Expérimentale
Chrome Support complet 46Edge Support complet 14Firefox Support complet OuiIE Aucun support NonOpera Support complet OuiSafari Aucun support NonWebView Android Aucun support NonChrome Android Support complet 46Firefox Android Support complet OuiOpera Android Support complet OuiSafari iOS Aucun support NonSamsung Internet Android Support complet 5.0
referrer
Expérimentale
Chrome Support complet 42
Support complet 42
Support complet 41
Désactivée
Désactivée From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Support complet 14Firefox Support complet 47IE Aucun support NonOpera Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Aucun support NonWebView Android Aucun support NonChrome Android Aucun support NonFirefox Android Aucun support NonOpera Android Support complet 29
Support complet 29
Support complet 28
Désactivée
Désactivée From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS Aucun support NonSamsung Internet Android Aucun support Non
referrerPolicyChrome Support complet 52Edge Support complet 14Firefox Support complet 52IE Aucun support NonOpera Support complet 39Safari Support complet 11.1WebView Android Support complet 52Chrome Android Support complet 52Firefox Android Support complet 52Opera Android Support complet 41Safari iOS Aucun support NonSamsung Internet Android Support complet 7.2
signalChrome Support complet 66Edge Support complet 16Firefox Support complet OuiIE Aucun support NonOpera Support complet OuiSafari ? WebView Android Support complet 66Chrome Android Support complet 66Firefox Android Support complet OuiOpera Android Support complet OuiSafari iOS ? Samsung Internet Android Support complet 9.0
url
Expérimentale
Chrome Support complet 42
Notes
Support complet 42
Notes
Notes Fragment support added in Chrome 59.
Edge Support complet 14Firefox Support complet 39
Support complet 39
Support complet 34
Désactivée
Désactivée From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Aucun support NonOpera Support complet 29
Notes
Support complet 29
Notes
Notes Fragment support added in Opera 46.
Safari Support complet 10.1WebView Android Support complet 42
Notes
Support complet 42
Notes
Notes Fragment support added in Chrome 59.
Chrome Android Support complet 42
Notes
Support complet 42
Notes
Notes Fragment support added in Chrome 59.
Firefox Android Aucun support NonOpera Android Support complet 29
Notes
Support complet 29
Notes
Notes Fragment support added in Opera 46.
Safari iOS Support complet 10.3Samsung Internet Android Support complet 4.0
Notes
Support complet 4.0
Notes
Notes Fragment support added in Samsung Internet 7.0.

Légende

Support complet  
Support complet
Aucun support  
Aucun support
Compatibilité inconnue  
Compatibilité inconnue
Fonctionnalité expérimentale. Celle-ci peut être amenée à changer par la suite.
Fonctionnalité expérimentale. Celle-ci peut être amenée à changer par la suite.
Fonctionnalité non-standard. Celle-ci peut être incorrectement supportée par les autres navigateurs.
Fonctionnalité non-standard. Celle-ci peut être incorrectement supportée par les autres navigateurs.
Obsolète. Les nouveaux sites web ne doivent pas utiliser cette fonctionnalité.
Obsolète. Les nouveaux sites web ne doivent pas utiliser cette fonctionnalité.
Voir les notes d'implémentation.
Voir les notes d'implémentation.
Une action explicite de l'utilisateur est nécessaire pour activer cette fonctionnalité.
Une action explicite de l'utilisateur est nécessaire pour activer cette fonctionnalité.

See also