Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.

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.

Constructor

Request.Request()
Creates a new Request object.

Properties

Request.method Read only
Contains the request's method (GET, POST, etc.)
Request.url Read only
Contains the URL of the request.
Request.headers Read only
Contains the associated Headers object of the request.
Request.context Read only  
Contains the context of the request (e.g., audio, image, iframe, etc.)
Request.referrer Read only
Contains the referrer of the request (e.g., client).
Request.referrerPolicy Read only
Contains the referrer policy of the request (e.g., no-referrer).
Request.mode Read only
Contains the mode of the request (e.g., cors, no-cors, same-origin, navigate.)
Request.credentials Read only
Contains the credentials of the request (e.g., omit, same-origin).
Request.redirect Read only
Contains the mode for how redirects are handled. It may be one of follow, error, or manual.
Request.integrity Read only
Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
Request.cache Read only
Contains the cache mode of the request (e.g., default, reload, no-cache).

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

Body.body Read only
A simple getter used to expose a ReadableStream of the body contents.
Body.bodyUsed Read only
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 myRequest = new Request('http://localhost/flowers.jpg');

const myURL = myRequest.url; // http://localhost/flowers.jpg
const myMethod = myRequest.method; // GET
const myCred = myRequest.credentials; // omit

You could then fetch this request by passing the Request object in as a parameter to a GlobalFetch.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; // omit
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 GlobalFetch.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);
  });

Specifications

Specification Status Comment
Fetch
La definición de 'Request' en esta especificación.
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Soporte básico
Experimental
Chrome Soporte completo 42Edge Soporte completo SiFirefox Soporte completo 39IE Sin soporte NoOpera Soporte completo 28Safari Sin soporte NoWebView Android Soporte completo 42Chrome Android Soporte completo 42Edge Mobile Soporte completo SiFirefox Android Soporte completo SiOpera Android Soporte completo 28Safari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
Request() constructor
Experimental
Chrome Soporte completo 42
Soporte completo 42
Soporte completo 41
Notas
Notas Some default values for the init parameter changed in Chrome 47. See the Properties section for details.
Edge Soporte completo 15Firefox Soporte completo 39
Soporte completo 39
Soporte completo 34
Deshabilitado
Deshabilitado From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Sin soporte NoOpera Soporte completo 29
Soporte completo 29
Soporte completo 28
Deshabilitado
Deshabilitado From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Soporte completo 10.1WebView Android Soporte completo 42
Soporte completo 42
Soporte completo 41
Notas
Notas Some default values for the init parameter changed in Chrome 47. See the Properties section for details.
Chrome Android Soporte completo 42
Soporte completo 42
Soporte completo 41
Notas
Notas Some default values for the init parameter changed in Chrome 47. See the Properties section for details.
Edge Mobile Soporte completo SiFirefox Android Soporte completo SiOpera Android Sin soporte NoSafari iOS Soporte completo 10.3Samsung Internet Android Soporte completo 4.0
Soporte completo 4.0
Soporte completo 5.0
Notas
Notas Some default values for the init parameter changed in Samsung Internet 5.0. See the Properties section for details.
cache
Experimental
Chrome Sin soporte No
Notas
Sin soporte No
Notas
Notas Nightly build
Edge Soporte completo SiFirefox Soporte completo 48IE Sin soporte NoOpera Sin soporte NoSafari Soporte completo 11WebView Android Sin soporte NoChrome Android Sin soporte NoEdge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Sin soporte No
clone
Experimental
Chrome Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Soporte completo SiFirefox Soporte completo 39
Soporte completo 39
Soporte completo 34
Deshabilitado
Deshabilitado From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Sin soporte NoOpera Soporte completo 29
Soporte completo 29
Soporte completo 28
Deshabilitado
Deshabilitado From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Sin soporte NoWebView Android Sin soporte NoChrome Android Sin soporte NoEdge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Sin soporte No
context
ExperimentalDeprecatedNo estándar
Chrome Sin soporte 42 — 46Edge ? Firefox Sin soporte 39 — 42
Notas
Sin soporte 39 — 42
Notas
Notas see bug 1188062 for more information.
IE Sin soporte NoOpera Sin soporte 28 — 29Safari Sin soporte NoWebView Android Sin soporte 42 — 46Chrome Android Sin soporte 42 — 46Edge Mobile ? Firefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
credentials
Experimental
Chrome Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Soporte completo SiFirefox Soporte completo 39
Soporte completo 39
Soporte completo 34
Deshabilitado
Deshabilitado From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Sin soporte NoOpera Soporte completo 29
Soporte completo 29
Soporte completo 28
Deshabilitado
Deshabilitado From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Sin soporte NoWebView Android Sin soporte NoChrome Android Sin soporte 42 — 46Edge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
destination
Experimental
Chrome Soporte completo 65Edge Soporte completo 14Firefox Soporte completo 61IE Sin soporte NoOpera Soporte completo 52Safari Sin soporte NoWebView Android ? Chrome Android Soporte completo 65Edge Mobile ? Firefox Android Soporte completo 61Opera Android Soporte completo 52Safari iOS Sin soporte NoSamsung Internet Android ?
headers
Experimental
Chrome Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Soporte completo SiFirefox Soporte completo 39
Soporte completo 39
Soporte completo 34
Deshabilitado
Deshabilitado From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Sin soporte NoOpera Soporte completo 29
Soporte completo 29
Soporte completo 28
Deshabilitado
Deshabilitado From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Sin soporte NoWebView Android Sin soporte NoChrome Android Sin soporte 42 — 46Edge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
integrity
ExperimentalDeprecated
Chrome Soporte completo 46Edge Soporte completo SiFirefox Soporte completo SiIE Sin soporte NoOpera Soporte completo SiSafari Sin soporte NoWebView Android Sin soporte NoChrome Android Soporte completo 46Edge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Soporte completo SiSafari iOS Sin soporte NoSamsung Internet Android Soporte completo 5.0
keepalive
ExperimentalDeprecated
Chrome Soporte completo 66Edge Soporte completo 15Firefox ? IE ? Opera Soporte completo 43Safari ? WebView Android Sin soporte NoChrome Android Soporte completo 66Edge Mobile ? Firefox Android ? Opera Android Soporte completo 43Safari iOS ? Samsung Internet Android Sin soporte No
method
Experimental
Chrome Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Soporte completo SiFirefox Soporte completo 39
Soporte completo 39
Soporte completo 34
Deshabilitado
Deshabilitado From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Sin soporte NoOpera Soporte completo 29
Soporte completo 29
Soporte completo 28
Deshabilitado
Deshabilitado From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Sin soporte NoWebView Android Sin soporte NoChrome Android Sin soporte 42 — 46Edge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
mode
Experimental
Chrome Soporte completo 42Edge Soporte completo SiFirefox Soporte completo 39IE Sin soporte NoOpera Soporte completo 29Safari Sin soporte NoWebView Android Soporte completo 49Chrome Android Soporte completo 49Edge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Soporte completo 5.0
redirect
Experimental
Chrome Soporte completo 46Edge Soporte completo SiFirefox Soporte completo SiIE Sin soporte NoOpera Soporte completo SiSafari Sin soporte NoWebView Android Sin soporte NoChrome Android Soporte completo 46Edge Mobile Soporte completo SiFirefox Android Soporte completo SiOpera Android Soporte completo SiSafari iOS Sin soporte NoSamsung Internet Android Soporte completo 5.0
referrer
Experimental
Chrome Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Soporte completo SiFirefox Soporte completo 47IE Sin soporte NoOpera Soporte completo 29
Soporte completo 29
Soporte completo 28
Deshabilitado
Deshabilitado From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Sin soporte NoWebView Android Sin soporte NoChrome Android Sin soporte NoEdge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte NoSafari iOS Sin soporte NoSamsung Internet Android Sin soporte No
referrerPolicyChrome Soporte completo 52Edge Sin soporte NoFirefox Soporte completo 52IE Sin soporte NoOpera Soporte completo 39Safari Soporte completo 11.1WebView Android Soporte completo 52Chrome Android Soporte completo 52Edge Mobile Sin soporte NoFirefox Android Soporte completo 52Opera Android Soporte completo 39Safari iOS Sin soporte NoSamsung Internet Android Soporte completo 7.2
signalChrome ? Edge Soporte completo 16Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
url
Experimental
Chrome Soporte completo 42
Notas
Soporte completo 42
Notas
Notas Fragment support added in Chrome 59.
Edge Soporte completo SiFirefox Soporte completo 39
Soporte completo 39
Soporte completo 34
Deshabilitado
Deshabilitado From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE Sin soporte NoOpera Soporte completo 29
Notas
Soporte completo 29
Notas
Notas Fragment support added in Opera 46.
Safari Sin soporte NoWebView Android Soporte completo 42
Notas
Soporte completo 42
Notas
Notas Fragment support added in Chrome 59.
Chrome Android Soporte completo 42
Notas
Soporte completo 42
Notas
Notas Fragment support added in Chrome 59.
Edge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Soporte completo 29
Notas
Soporte completo 29
Notas
Notas Fragment support added in Opera 46.
Safari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0

Leyenda

Soporte completo  
Soporte completo
Sin soporte  
Sin soporte
Compatibility unknown  
Compatibility unknown
Experimental. Esperar que el comportamiento cambie en el futuro.
Experimental. Esperar que el comportamiento cambie en el futuro.
No estandar . Esperar poco soporte entre navegadores.
No estandar . Esperar poco soporte entre navegadores.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
Ver notas de implementación.
Ver notas de implementación.
El usuario debe de habilitar explícitamente esta característica.
El usuario debe de habilitar explícitamente esta característica.

See also

Etiquetas y colaboradores del documento

Colaboradores en esta página: fscholz
Última actualización por: fscholz,