W3cubDocs

/DOM

Headers

The Headers interface of the Fetch API allows you to perform various actions on HTTP request and response headers. These actions include retrieving, setting, adding to, and removing. A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs. You can add to this using methods like append() (see Examples.) In all methods of this interface, header names are matched by case-insensitive byte sequence.

For security reasons, some headers can only be controlled by the user agent. These headers include the forbidden header names and forbidden response header names.

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
The definition of 'Headers' in that specification.
Living Standard

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
10.1
Lexicographical sorting, and values from duplicate header names combined when iterated. ? ? 44 No 24 No
Headers() constructor 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
? 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
10.1
append 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
10.1
delete 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
10.1
entries 45 16 44 ? 32 ?
get 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes 52
52
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.
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
10.1
getAll 42 — 60 Yes 39 — 52
39 — 52
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29 — 47 No
has 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No
keys 45 16 44 ? 32 ?
set 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No
values 45 16 44 ? 32 ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
? 44 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No 4.0
Lexicographical sorting, and values from duplicate header names combined when iterated. ? ? ? No ? No ?
Headers() constructor 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
? No 29
29
28
Disabled
Disabled From version 28: this feature is behind the preference.
No 4.0
append 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No 4.0
delete 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No 4.0
entries 45 45 Yes 44 32 ? 5.0
get 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No 4.0
getAll 42 — 60 42 — 60 Yes No 29 — 47 No 4.0
has 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No 4.0
keys 45 45 Yes 44 32 ? 5.0
set 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Yes No 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
No 4.0
values 45 45 Yes 44 32 ? 5.0

See also

© 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/Headers