Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Function Trigger
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • Sample Functions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
    • Best Practices
      • Adaptive Image Format Conversion via Edge Functions

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 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.

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.

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