Headers

Headers Class The `Headers` class represents HTTP headers.

Constructor

new Headers(init)

Creates a new instance of the Headers class.
Parameters:
NameTypeDescription
initobjectOptional. An initial object to populate the headers.
Properties
NameTypeAttributesDefaultDescription
init.headersobject<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:
NameTypeDescription
namestringThe name of the header.
valuestringThe value to append to the header.

delete(name)

Deletes the specified header.
Parameters:
NameTypeDescription
namestringThe 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:
NameTypeAttributesDescription
callbackfunctionThe callback function to execute for each header.
thisArgobject<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:
NameTypeDescription
namestringThe 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:
NameTypeDescription
namestringThe 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:
NameTypeDescription
namestringThe name of the header.
valuestringThe 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