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

La interfaz Headers de la Fetch API permite realizar diversas acciones en los Headers de solicitud y respuesta HTTP.Estas acciones incluyen recuperar, establecer, agregar y eliminar. Un objeto Header tiene una lista  asociada que inicialmente está vacía, y consta de cero o más pares de nombre y valor.
Es posible añadir metodos de uso como append() (ver ejemplos.) En todos los métodos de esta interfaz, los nombres de los encabezados se relacionan con una secuencia de bytes sensible a mayúsculas y minúsculas.
Por razones de seguridad, algunos headers pueden ser controlados unicamente por el user agent. Estos headers incluyen los nombres prohibidos para headers  y nombres prohibidos de Headers response.

A Headers object also has an associated guard, which takes a value of immutable, request, request-no-cors, response, or none. This affects whether the set(), delete(), and append() methods will mutate the header. For more information see Guard.

You can retrieve a Headers object via the Request.headers and Response.headers properties, and create a new Headers object using the Headers.Headers() constructor.

An object implementing Headers can directly be used in a for...of structure, instead of entries(): for (var p of myHeaders) is equivalent to for (var p of myHeaders.entries()).

Note: you can find more out about the available headers by reading our HTTP headers reference.

Constructor

Headers.Headers()
Creates a new Headers object.

Methods

Headers.append()
Appends a new value onto an existing header inside a Headers object, or adds the header if it does not already exist.
Headers.delete()
Deletes a header from a Headers object.
Headers.entries()
Returns an iterator allowing to go through all key/value pairs contained in this object.
Headers.forEach()
Executes a provided function once for each array element.
Headers.get()
Returns a ByteString sequence of all the values of a header within a Headers object with a given name.
Headers.has()
Returns a boolean stating whether a Headers object contains a certain header.
Headers.keys()
Returns an iterator allowing you to go through all keys of the key/value pairs contained in this object.
Headers.set()
Sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist.
Headers.values()
Returns an iterator allowing you to go through all values of the key/value pairs contained in this object.

Note: To be clear, the difference between Headers.set() and Headers.append() is that if the specified header does already exist and does accept multiple values, Headers.set() will overwrite the existing value with the new one, whereas Headers.append() will append the new value onto the end of the set of values. See their dedicated pages for example code.

Note: All of the Headers methods will throw a TypeError if you try to pass in a reference to a name that isn't a valid HTTP Header name. The mutation operations will throw a TypeError if the header has an immutable Guard. In any other failure case they fail silently.

Note: When Header values are iterated over, they are automatically sorted in lexicographical order, and values from duplicate header names are combined.

Obsolete methods

Headers.getAll()
Used to return an array of all the values of a header within a Headers object with a given name; this method has now been deleted from the spec, and Headers.get() now returns all values instead of just one.

Examples

In the following snippet, we create a new header using the Headers() constructor, add a new header to it using append(), then return that header value using get():

var myHeaders = new Headers();

myHeaders.append('Content-Type', 'text/xml');
myHeaders.get('Content-Type') // should return 'text/xml'

The same can be achieved by passing an array of arrays or an object literal to the constructor:

var myHeaders = new Headers({
    'Content-Type': 'text/xml'
});

// or, using an array of arrays:
myHeaders = new Headers([
    ['Content-Type', 'text/xml']
]);

myHeaders.get('Content-Type') // should return 'text/xml'

Specifications

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

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Soporte básicoChrome 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 Soporte completo 10.1WebView Android Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android 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 Mobile ? Firefox Android Soporte completo 44Opera Android 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 iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
Lexicographical sorting, and values from duplicate header names combined when iterated.Chrome ? Edge ? Firefox Soporte completo 44IE Sin soporte NoOpera Soporte completo 24Safari Sin soporte NoWebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Sin soporte NoOpera Android ? Safari iOS Sin soporte NoSamsung Internet Android ?
Headers() constructor
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 ? Firefox 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
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android 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 Mobile ? Firefox Android Sin soporte NoOpera Android Soporte completo 29
Soporte completo 29
Soporte completo 28
Deshabilitado
Deshabilitado From version 28: this feature is behind the preference.
Safari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
append
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 Soporte completo 10.1WebView Android Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android 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 Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android 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 iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
delete
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 Soporte completo 10.1WebView Android Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android 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 Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android 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 iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
entries
Experimental
Chrome Soporte completo 45Edge Soporte completo 16Firefox Soporte completo 44IE ? Opera Soporte completo 32Safari ? WebView Android Soporte completo 45Chrome Android Soporte completo 45Edge Mobile Soporte completo SiFirefox Android Soporte completo 44Opera Android Soporte completo 32Safari iOS ? Samsung Internet Android Soporte completo 5.0
get
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 52
Notas
Soporte completo 52
Notas
Notas Prior to Firefox 52, get() only returned the first value in the specified header, with getAll() returning all values. From 52 onwards, get() now returns all values and getAll() has been deleted.
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
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android 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 Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android 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 iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
getAll
ExperimentalDeprecatedNo estándar
Chrome Sin soporte 42 — 60Edge Soporte completo SiFirefox Sin soporte 39 — 52
Sin soporte 39 — 52
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 Sin soporte 29 — 47Safari Sin soporte NoWebView Android Sin soporte 42 — 60Chrome Android Sin soporte 42 — 60Edge Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android Sin soporte 29 — 47Safari iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
has
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 Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android 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 Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android 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 iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
keys
Experimental
Chrome Soporte completo 45Edge Soporte completo 16Firefox Soporte completo 44IE ? Opera Soporte completo 32Safari ? WebView Android Soporte completo 45Chrome Android Soporte completo 45Edge Mobile Soporte completo SiFirefox Android Soporte completo 44Opera Android Soporte completo 32Safari iOS ? Samsung Internet Android Soporte completo 5.0
set
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 Soporte completo 42
Soporte completo 42
Soporte completo 41
Deshabilitado
Deshabilitado From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android 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 Mobile Soporte completo SiFirefox Android Sin soporte NoOpera Android 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 iOS Sin soporte NoSamsung Internet Android Soporte completo 4.0
values
Experimental
Chrome Soporte completo 45Edge Soporte completo 16Firefox Soporte completo 44IE ? Opera Soporte completo 32Safari ? WebView Android Soporte completo 45Chrome Android Soporte completo 45Edge Mobile Soporte completo SiFirefox Android Soporte completo 44Opera Android Soporte completo 32Safari iOS ? Samsung Internet Android Soporte completo 5.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: Estebanrg21
Última actualización por: Estebanrg21,