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 | 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.bodyreadonly body: ReadableStream;
bodyUsed
// request.bodyUsedreadonly bodyUsed: boolean;
Indicates whether the request body is read.
headers
// request.headersreadonly headers: Headers;
method
// request.methodreadonly method: string;
The request method. Default value:
GET
.redirect
// request.redirectreadonly redirect: string;
The request redirect mode. Valid values:
follow
, error
, and manual
. Default value: manual
.maxFollow
// request.maxFollowreadonly maxFollow: number;
The maximum number of redirects.
url
// request.urlreadonly url: string;
The request URL.
version
// request.versionreadonly version: string;
The HTTP version that is used by the request.
eo
// request.versionreadonly eo: IncomingRequestEoProperties;
Other information provided by Edge Functions about the current request. For more information, see IncomingRequestEoProperties.
IncomingRequestEoProperties
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>;
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());});