requests.do() 函数
requests.do()
发起一个 http 请求。
返回的响应包含以下属性
- statusCode: 从请求返回的 HTTP 状态码。
- body: 请求的内容。将从响应主体中读取最大 100MB 的大小。
- headers: 响应中存在的标头。
- duration: 请求的持续时间。
函数类型签名
(
method: string,
url: string,
?body: bytes,
?config: {A with timeout: duration, insecureSkipVerify: bool},
?headers: [string:string],
?params: [string:[string]],
) => {statusCode: int, headers: [string:string], duration: duration, body: bytes}
有关更多信息,请参阅 函数类型签名。
参数
method
(必需) http 请求的方法。支持的方法:DELETE、GET、HEAD、PATCH、POST、PUT。
url
(必需) 要请求的 URL。这不应包含任何查询参数。
params
要添加到 URL 作为查询参数的键值对集合。查询参数将进行 URL 编码。键的所有值都将附加到查询中。
headers
要包含在请求中的键值对集合。
body
要与请求一起发送的数据。
config
用于控制应如何执行请求的选项集。
示例
发起一个 GET 请求
import "http/requests"
response = requests.do(url: "http://example.com", method: "GET")
requests.peek(response: response)
发起一个需要授权的 GET 请求
import "http/requests"
import "influxdata/influxdb/secrets"
token = secrets.get(key: "TOKEN")
response =
requests.do(
method: "GET",
url: "http://example.com",
headers: ["Authorization": "token ${token}"],
)
requests.peek(response: response)
发起一个带有查询参数的 GET 请求
import "http/requests"
response = requests.do(method: "GET", url: "http://example.com", params: ["start": ["100"]])
requests.peek(response: response)
此页是否对您有帮助?
感谢您的反馈!