Headers()

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The Headers() constructor creates a new Headers object.

Syntax

JavaScript
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:

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

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

JavaScript
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:

JavaScript
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:

JavaScript
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

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 42
41 behind pref
 
39 (39)
34 behind pref
No support

29
28 behind pref

No support
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support No support No support No support No support No support No support

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/headers/headers

API Constructor Experimental Expérimental Fetch Reference Référence