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

Request

The Request API represents an HTTP request. It is designed based on the standard Web API Request.
Note
In Edge Functions, you can obtain a Request object by using any of the following methods:
Create a Request object for the Fetch API by using the Request constructor.
Use the FetchEvent object event.request to obtain a Request object.

Constructor API

const request = new Request(input: string | Request, init?: RequestInit)

Parameters

Parameter name
Type
Required
Description
input
string | Request
Yes
A URL string or a Request object.
options
No
Initial configuration items of the Request object.

RequestInit

The following table describes the initial configuration items of the Request object.
Name
Type
Required
Default value
Description
method
string
No
GET
Request method. Examples: GET and POST.
headers
No
-
Headers that you want to add to the request.
body
string |Blob | ArrayBuffer | ArrayBufferView |ReadableStream
No
-
Request body.
redirect
string
No
follow
Redirect mode. Valid values: manual, error, and follow.
maxFollow
number
No
12
The maximum number of redirects allowed.
version
string
No
HTTP/1.1
HTTP version. Valid values: HTTP/1.0, HTTP/1.1, and HTTP/2.0.
copyHeaders
boolean
No
-
Specifies whether to copy the headers of the Request object. This parameter is not in the Web API specifications.
eo
No
-
Specifies the behavior that Edge Functions adopts in processing the request. This parameter is not in the Web API specifications.

RequestInitEoProperties

This parameter specifies the behavior that Edge Functions adopts in processing the request. It is not in the Web API specifications.
Parameter name
Type
Required
Description
resolveOverride
string
No
Overrides the original domain name resolution for the fetch request. You can specify a domain name or IP address that meets the following requirements:
The IP address cannot contain a scheme or port number.
For an IPv6 address, you do not need to enclose it in a pair of square brackets.
Image
No
Image processing configurations

ImageProperties

fetch supports image processing. The configurations are the same as of site acceleration. For more information, see Resizing and Converting Images.
Parameter name
Type
Required
Description
format
string
No
Convert an image into a specified format. Valid values: jpg, gif, png, bmp, webp, avif, jp2, jxr, heif.
long
number
No
Specify the length of the long side, and automatically scale the short side (if not specified)
short
number
No
Specify the length of the short side, and automatically scale the long side (if not specified)
width
number
No
Specify the width, and automatically scale the height (if not specified)
height
number
No
Specify the height, and automatically scale the width (if not specified)

Attributes

body

// request.body
readonly body: ReadableStream;
Request body. For more information, see ReadableStream.

bodyUsed

// request.bodyUsed
readonly bodyUsed: boolean;
Indicates whether the request body is read.

headers

// request.headers
readonly headers: Headers;
Request headers. For more information, see Headers.

method

// request.method
readonly method: string;
The request method. Default value: GET.

redirect

// request.redirect
readonly redirect: string;
The request redirect mode. Valid values: follow, error, and manual. Default value: manual.

maxFollow

// request.maxFollow
readonly maxFollow: number;
The maximum number of redirects.

url

// request.url
readonly url: string;
The request URL.

version

// request.version
readonly version: string;
The HTTP version that is used by the request.

eo

// request.version
readonly eo: IncomingRequestEoProperties;
Other information provided by Edge Functions about the current request. For more information, see IncomingRequestEoProperties.

IncomingRequestEoProperties

The client request object event.request contains an eo attribute.
Name
Type
Description
Example
geo
Location of the client who initiates the request.
-

GeoProperties

Location of the client who initiates the request.
Name
Type
Description
Example
asn
number
132203
countryName
string
Country name
Singapore
countryCodeAlpha2
string
ISO-3611 alpha2 code of the country
SG
countryCodeAlpha3
string
ISO-3611 alpha3 code of the country
SGP
countryCodeNumeric
string
ISO-3611 numeric code of the country.
702
regionName
string
Region name
-
regionCode
string
Region code
AA-AA
cityName
string
City name
singapore
latitude
number
Latitude
1.29027
longitude
number
Longitude
103.851959

Methods

Important
When using a method to obtain the request body, the size of the HTTP body is capped at 1 MB. If the threshold is exceeded, an OverSize exception is returned. In this case, we recommend that you use request.body to read the request body in streaming mode. For more information, see ReadableStream.

arrayBuffer

request.arrayBuffer(): Promise<ArrayBuffer>;
The arrayBuffer() method reads the request body and returns a promise that resolves with an ArrayBuffer.

blob

request.blob(): Promise<Blob>;
The blob() method reads the request body and returns a promise that resolves with a Blob.

clone

request.clone(copyHeaders?: boolean): Request;
The clone() method creates a clone of a request object.

Parameter

Parameter name
Type
Required
Description
copyHeaders
boolean
No
Specifies whether to copy the request headers of the original object. Default value: false. Valid values:
true
Copy the request headers of the original object.
false
Reference the request headers of the original object.

json

request.json(): Promise<object>;
The json() method reads the request body and returns a promise that resolves with the parsing result of the body text as json.

text

request.text(): Promise<string>;
The text() method reads the request body and returns a promise that resolves with a String.

formData

request.formData(): Promise<FormData>;
The formData() method takes a Response stream, reads it to completion, and returns a promise that resolves with a FormData.


Parameter

Parameter name
Type
Required
Description
cookies
No
A new Cookies object.

Sample Code

async function handleRequest() {
const request = new Request('https://www.tencentcloud.com/');
const response = await fetch(request);
return response;
}

addEventListener('fetch', (event) => {
event.respondWith(handleRequest());
});

References