tencent cloud

All product documents
Tencent Cloud EdgeOne
Last updated: 2025-03-04 15:37:54
Headers
Last updated: 2025-03-04 15:37:54
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 API

const headers = new Headers(init?: object | Array<[string, string]> | Headers);

Parameters

Parameter
Type
Required
Description
init
No
The HTTP header that is used to pre-populate the Headers object. Valid parameter types:
object
The Constructor API enumerates all enumerable attributes that are included in the specified object and pre-populates the attributes to the 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
The Constructor API copies all fields from an existing Headers object to the 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 cannot exceed 255 bytes, and can only consist of digits, uppercase and lowercase letters, -, and _. Otherwise, an exception will be thrown.
The value cannot exceed 4,095 bytes. Leading and trailing whitespace characters '\\n', '\\r', '\\t', and space will be automatically removed, and the string cannot contain '\\0', '\\n', or '\\r' in the middle. Otherwise, an exception will be thrown.

Parameters

Parameter
Type
Required
Description
name
string
Yes
The name of the HTTP header that you want to add to the Headers object.
value
string
Yes
The value of the HTTP header that you want to add.


delete

headers.delete(name: string): void;
The delete() method deletes the specified header from the Headers object.

Parameters

Parameter
Type
Required
Description
name
string
Yes
The name of the HTTP header that you want to delete from the Headers object.


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.
Note:
The forEach method does not comply with Web API standards. Edge Functions extends the functionality of the method based on Web API standards to provide an efficient way to traverse headers.

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>
This method returns an array, containing all values of the Set-Cookie headers.

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 cannot exceed 255 bytes, and can only consist of digits, uppercase and lowercase letters, -, and _. Otherwise, an exception will be thrown.
The value cannot exceed 4,095 bytes. Leading and trailing whitespace characters '\\n', '\\r', '\\t', and space will be automatically removed, and the string cannot 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));
});

References

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon