Request

最近更新时间:2023-03-09 15:26:48

我的收藏
Request 是发起请求时的请求结构。

字段

字段
类型
描述
basicAuth?
BasicAuth
基础鉴权
body?
string、object 或 ArrayBuffer
要发送的请求体,在使用 http.do 方法时才需要指定
chunked?
function,(body string) => void
当数据以一系列分块的形式进行发送时,如果指定了 chunked 函数,会按行读取响应体并进行回调函数的运行
contentLength?
number
记录关联内容的长度;-1 表示长度未知,>=0 表示可以从 body 中读取给定的字节数
discardResponseBody?
boolean
丢弃响应体,适用于响应体太大且不需要对象应体内容进行 check 的场景
headers?
Record<string, string>
请求头
host?
string
host 或 host:port
maxRedirects?
number
最大重定向跳转次数
method?
string
指定 HTTP 方法,如 GET、PUT、POST 等,只有当使用 http.do 方法时才需要指定
path?
string
路径,相对路径省略前导斜杠
query?
Record<string, string>
与请求一同发送的 URL 参数
scheme?
"http" | "https"
协议,填写 "http" 或 "https"
service?
string
在 PTS 中,按照 url 识别不同 service 并基于该维度生成报表;如当 url 为 http://demo.com/{id},不同的 id 会被识别为不同的 service,可以通过指定 service,将此类请求在报表中归类到同一个服务下
timeout?
number
客户端发出的请求的时间限制,超时包括连接时间、任何重定向和读取响应正文,单位为毫秒
url?
string
要访问的 URL,只有当使用 do 方法时才需要指定

样例

在 http.do 方法中使用 Request:
import http from 'pts/http';

export default function () {
const req = {
method: 'post',
url: 'http://mockhttpbin.pts.svc.cluster.local/post',
headers: { 'Content-Type': 'application/json' },
query: { a: '1' },
body: { user_id: '12345' },
};
const resp = http.do(req);
console.log(resp.json().args.a); // 1
console.log(resp.json().json.user_id); // 12345
}