The Headers() constructor creates a new Headers object.

Syntax

var myHeaders = new Headers(init);

Parameters

init Optional
An object containing any HTTP headers that you want to pre-populate your Headers object with. This can be a simple object literal with ByteString values; or an existing Headers object. In the last case, the new Headers object inherits its data from the existing Headers object.

Example

Creating an empty Headers object is simple:

var myHeaders = new Headers(); // Currently empty

You could add a header to this using Headers.append:

myHeaders.append('Content-Type', 'image/jpeg');
myHeaders.get('Content-Type'); // Returns 'image/jpeg'

Or you can add the headers you want as the Headers object is created. In the following snippet we create a new Headers object, adding some headers by passing the constructor an init object as an argument:

var httpHeaders = { 'Content-Type' : 'image/jpeg', 'Accept-Charset' : 'utf-8', 'X-My-Custom-Header' : 'Zeke are cool' };
var myHeaders = new Headers(httpHeaders);

You can now create another Headers object, passing it the first Headers object as its init object:

var secondHeadersObj = new Headers(myHeaders);
secondHeadersObj.get('Content-Type'); // Would return 'image/jpeg' — it inherits it from the first headers object

Specifications

Specification Status Comment
Fetch
The definition of 'Headers()' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support

42

412

?

39

343

No

29

284

10.1
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support

42

411

42

412

? No

29

285

No4.0

1. From version 41: this feature is behind the Experimental Web Platform Features preference.

2. From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.

3. From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.

4. From version 28: this feature is behind the Experimental Web Platform Features preference.

5. From version 28: this feature is behind the preference.

See also

Document Tags and Contributors

 Contributors to this page: fscholz, soswow, chrisdavidmills, runn1ng, xcatliu, jpmedley, kscarfone
 Last updated by: fscholz,