Constructor
new Headers(init)
Creates a new instance of the Headers class.
Parameters:
PropertiesName | Type | Description |
---|---|---|
init | object | Optional. An initial object to populate the headers. |
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
init.headers | object | <optional> | {} | The headers of the request. |
Example
// Headers Use
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer token123');
headers.set('Content-Length', '1024');
console.log(headers.get('Content-Type')); // Output: 'application/json'
console.log(headers.has('Authorization')); // Output: true
headers.delete('Authorization');
console.log(headers.has('Authorization')); // Output: false
headers.forEach((value, name) => {
console.log(name + ' ' + value); // Output: 'Content-Type application/json' then 'Content-Length 1024'
});
Methods
append(name, value)
Appends a value to the specified header.
Parameters:
Name | Type | Description |
---|---|---|
name | string | The name of the header. |
value | string | The value to append to the header. |
delete(name)
Deletes the specified header.
Parameters:
Name | Type | Description |
---|---|---|
name | string | The name of the header to delete. |
encode() → {Array}
Encodes the headers into a serializable format.
Returns:
- The encoded representation of the headers.
- Type:
- Array
entries() → {HeadersIterator}
Returns an iterator that iterates over the entries of the headers.
Returns:
- An iterator for the entries of the headers.
- Type:
- HeadersIterator
forEach(callback, thisArgopt)
Calls a function for each header in the headers.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback | function | The callback function to execute for each header. | |
thisArg | object | <optional> | Optional. The value to use as `this` when executing the callback function. |
get(name) → {string|null}
Gets the value of the specified header.
Parameters:
Name | Type | Description |
---|---|---|
name | string | The name of the header. |
Returns:
- The value of the header, or null if the header is not found.
- Type:
- string |
null
has(name) → {boolean}
Checks if the specified header exists.
Parameters:
Name | Type | Description |
---|---|---|
name | string | The name of the header. |
Returns:
- True if the header exists, false otherwise.
- Type:
- boolean
keys() → {HeadersIterator}
Returns an iterator that iterates over the keys of the headers.
Returns:
- An iterator for the keys of the headers.
- Type:
- HeadersIterator
set(name, value)
Sets the value of the specified header.
Parameters:
Name | Type | Description |
---|---|---|
name | string | The name of the header. |
value | string | The value of the header. |
values() → {HeadersIterator}
Returns an iterator that iterates over the values of the headers.
Returns:
- An iterator for the values of the headers.
- Type:
- HeadersIterator