Headers
The Headers API is designed based on the standard Web API Headers. You can use this API to perform operations on HTTP request and response headers.
Constructor
const headers = new Headers(init?: object | Array<[string, string]> | Headers);
Parameters
Parameter Name | Type | Required | Description |
init | No | Initialize the Headers object. The parameter types are described as follows: object The constructor will enumerate all enumerable properties contained in Object and initialize them into a new Headers object. Array<[string, string]> Each element in the array is a key-value pair. Example: [key, value]. The Constructor API traverses the array and pre-populates the key-value pairs to the new Headers object. Headers Copies the Headers object and initializes all fields into a new Headers object. |
Methods
append
headers.append(name: string, value: string): void;
The append() method appends a new value to a header that is specified by the
Headers object. If the header does not exist, the append() method directly adds the header.Note:
The
headers object has the following write-in limits:The name length cannot exceed 255 bytes and can only consist of digits, uppercase and lowercase letters, hyphens (-), and underscores (_). Otherwise, an exception will be thrown.
The value length cannot exceed 4095 bytes. Leading and trailing whitespace characters, including '\n', '\r', '\t', and spaces, are automatically trimmed. The string must not contain '\0', '\n', or '\r' in the middle. Otherwise, an exception will be thrown.
Parameters
Property Name | Type | Required | Description |
name | string | Yes | header name |
value | string | Yes | New value to append |
delete
headers.delete(name: string): void;
The delete() method deletes the specified header from the
Headers object.Parameters
Property Name | Type | Required | Description |
name | string | Yes | header name |
entries
headers.entries(): iterator;
The entries() method obtains an array of all key-value pairs from the
Headers object. The key-value pairs are in the format of [name, value]. For valid return values, see Iteration protocols.forEach
headers.forEach(callback: (name: string, value: string) => void | number): void;
The forEach() method traverses all headers that are included in the
Headers object. If callback returns a non-zero value, traversal is stopped.get
headers.get(name: string): string;
The get() method obtains the value of the specified header from the
Headers object.getSetCookie
headers.getSetCookie(): Array<string>
has
headers.has(name: string): boolean;
The has() method checks whether the
Headers object contains the specified header.keys
headers.keys(): iterator;
The keys() method obtains all keys that are included in the
Headers object. For valid return values, see Iteration protocols.set
headers.set(name: string, value: string): void;
The set() method sets a new value for an existing header in the
Headers object. If the header does not exist, the set() method directly adds the header.Note:
The
headers object has the following write-in limits:The name length cannot exceed 255 bytes and can only consist of digits, uppercase and lowercase letters, hyphens (-), and underscores (_). Otherwise, an exception will be thrown.
The value length cannot exceed 4095 bytes. Leading and trailing whitespace characters, including '\n', '\r', '\t', and spaces, are automatically trimmed. The string must not contain '\0', '\n', or '\r' in the middle. Otherwise, an exception will be thrown.
values
headers.values(): iterator;
The values() method obtains all values that are included in the
Headers object. For valid return values, see Iteration protocols.Sample Code
function handleEvent() {const headers = new Headers({'my-header-x': 'hello world',});const response = new Response('hello world', {headers,});return response;}addEventListener('fetch', (event) => {event.respondWith(handleEvent(event));});