Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Web Debugging
      • Function Trigger
      • Environment Variable
      • Code Replica
    • 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
      • Example Overview
      • Obtaining Client URL Information
      • Customization Based on Client Geo Location
      • Obtaining Client Geo Location Information
      • Batch Redirect
      • URL rewrite based on regular expressions
      • 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
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • Practical Tutorial
      • Overview
      • Origin retrieval based on user IP/geographic location
        • EdgeOne Implementation of Session Persistence Based on Client IP Addresses
        • EdgeOne Implementation of Origin-Pull Based on Client's Geo Location
      • APK dynamic packaging
        • EdgeOne enables dynamic packaging of Android APKs.
          • Feature Overview
          • Step 1: Preprocess the Android APK Parent Package
          • Step 2: Write the Channel Information into the APK Package with EdgeOne Edge Functions
      • Canary Release and Region-specific Execution
      • Adaptive Image Format Conversion via Edge Functions
      • Two Ways to Implement CDN Origin-pull Via Edge Function: Fetch and Passthrough
  • KV Storage
    • Overview
    • Operation Guide
  • Edge reasoning
    • Edge Inference Overview
    • Quick Guide

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>
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 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));
});

References