边缘开发者平台
  • 边缘函数
    • 概述
    • 快速指引
    • 操作指引
      • 函数管理
      • Web调试
      • 触发配置
      • 环境变量
      • 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
    • 示例函数
      • 示例概述
      • 获取客户端URL信息
      • 基于客户端地理位置的自定义
      • 获取客户端地理位置信息
      • 批量重定向
      • URL rewrite based on regular expressions
      • 返回 HTML 页面
      • 返回 JSON
      • Fetch 远程资源
      • 请求头鉴权
      • 修改响应头
      • AB 测试
      • 设置 Cookie
      • 基于请求区域重定向
      • Cache API 使用
      • 缓存 POST 请求
      • 流式响应
      • 合并资源流式响应
      • 防篡改校验
      • m3u8 改写与鉴权
      • 图片自适应缩放
      • 图片自适应 WebP
      • 自定义 Referer 限制规则
      • 远程鉴权
      • HMAC 数字签名
      • 自定义下载文件名
      • 获取客户端 IP
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • 最佳实践
      • 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
      • 通过边缘函数实现自适应图片格式转换
      • 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

Request

Request 代表 HTTP 请求对象,基于 Web APIs 标准 Request 进行设计。
说明
边缘函数中,可通过两种方式获得 Request 对象:
使用 Request 构造函数创建一个 Request 对象,用于 Fetch API 的操作。
使用 FetchEvent 对象 event.request,获得当前请求的 Request 对象。

构造函数

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

参数

参数名称
类型
必填
说明
input
string | Request
是
URL 字符串或 Request 对象。
options
否
Request 对象初始化配置项。

RequestInit

初始化 Request 对象的属性值选项。
属性名
类型
必填
默认值
说明
method
string
否
GET
请求方法 (GET、POST 等)。
headers
否
-
请求头部信息。
body
string |Blob | ArrayBuffer | ArrayBufferView |ReadableStream
否
-
请求体。
redirect
string
否
follow
重定向策略,支持 manual、error和 follow。
maxFollow
number
否
12
最大可重定向次数。
version
string
否
HTTP/1.1
HTTP 版本,支持 HTTP/1.0、HTTP/1.1 和 HTTP/2.0。
copyHeaders
boolean
否
-
非 Web APIs 标准选项,表示是否拷贝传入的 Request 对象的 headers。
eo
否
-
非 Web APIs 标准选项,用于控制边缘函数处理该请求的行为。

RequestInitEoProperties

非 Web APIs 标准选项,用于控制边缘函数处理该请求的行为。
参数名称
类型
必填
说明
resolveOverride
string
否
用于 fetch 请求下覆盖原有的域名解析, 支持指定域名或者 IP 地址。更多说明如下:
IP 不允许带 scheme 以及端口号。
IPv6 无需使用方括号包裹。
image
否
图片处理参数配置项。

ImageProperties

fetch 支持图片处理功能,参数配置项说明如下。该图片处理参数配置项与站点加速图片处理参数能力相同,您可以参考 图片处理了解站点加速内的图片处理能力。
参数名称
类型
必填
说明
format
string
否
将原图转换为指定格式,支持 jpg、gif、png、bmp、webp、avif、jp2、jxr、heif。
long
number
否
指定长边,短边未指定时,短边自适应。
short
number
否
指定短边,长边未指定时,长边自适应。
width
number
否
指定宽度,高度未指定时,高度自适应。
height
number
否
指定高度,宽度未指定时,宽度自适应。

实例属性

body

// request.body
readonly body: ReadableStream;
请求体,详情参见 ReadableStream。

bodyUsed

// request.bodyUsed
readonly bodyUsed: boolean;
标识请求体是否已读取。

headers

// request.headers
readonly headers: Headers;
请求头部,详情参见 Headers。

method

// request.method
readonly method: string;
请求方法,默认值为GET。

redirect

// request.redirect
readonly redirect: string;
请求重定向策略,可取值有:follow、error、manual,默认为 follow。

maxFollow

// request.maxFollow
readonly maxFollow: number;
请求最大重定向次数。

url

// request.url
readonly url: string;
请求 url。

version

// request.version
readonly version: string;
请求使用的 HTTP 协议版本。

eo

// request.eo
readonly eo: IncomingRequestEoProperties;
边缘函数提供的与当前请求相关的一些其他信息,详情参见 IncomingRequestEoProperties。

IncomingRequestEoProperties

客户端请求 event.request 对象包含 eo 属性,其信息如下:
属性名
类型
说明
示例值
geo
描述客户请求的位置信息。
-
clientIp
string
客户端IP地址
192.168.1.1
uuid
string
请求的唯一标识,作用同 EO-LOG-UUID
-

GeoProperties

描述客户请求的位置信息。
属性名
类型
说明
示例值
asn
number
132203
countryName
string
国家名
Singapore
countryCodeAlpha2
string
国家的 ISO-3166 alpha2 代码
SG
countryCodeAlpha3
string
国家的 ISO-3166 alpha3 代码
SGP
countryCodeNumeric
string
国家的 ISO-3166 numeric 代码
702
regionName
string
区域名
-
regionCode
string
区域代码
AA-AA
cityName
string
城市名
singapore
latitude
number
纬度
1.29027
longitude
number
经度
103.851959

实例方法

注意
获取请求体的方法,接收 HTTP body 最大字节数为 1M,超出大小会抛出 OverSize 异常。超出大小时推荐使用 request.body 流式读取,详情参见 ReadableStream。

arrayBuffer

request.arrayBuffer(): Promise<ArrayBuffer>;
获取请求体,解析结果为 ArrayBuffer。

blob

request.blob(): Promise<Blob>;
获取请求体,解析结果为 Blob。

clone

request.clone(copyHeaders?: boolean): Request;
创建请求对象的副本。

参数

参数名称
类型
必填
说明
copyHeaders
boolean
否
开启复制请求头,默认值为 false,取值说明如下。
true
复制原对象的请求头。
false
引用原对象的请求头。

json

request.json(): Promise<object>;
获取请求体,解析结果为 json。

text

request.text(): Promise<string>;
获取请求体,解析结果为文本字符串。

formData

request.formData(): Promise<FormData>;
获取响应体,解析结果为 FormData。

参数

参数名称
类型
必填
说明
cookies
否
新的 Cookies 对象。

示例代码

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

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

相关参考