The Headers() constructor creates a new Headers object.
Syntax
var myHeaders = new Headers(init);
Parameters
initOptional- An object containing any HTTP headers that you want to pre-populate your
Headersobject with. This can be a simple object literal withByteStringvalues; or an existingHeadersobject. In the last case, the newHeadersobject inherits its data from the existingHeadersobject.
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 |
| FetchThe definition of 'Headers()' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
No compatibility data found. Please contribute data for "api.Headers.headers" (depth: 1) to the MDN compatibility data repository.
See also
Headers() by Mozilla Contributors is licensed under CC-BY-SA 2.5.