请求( method, url, [body], [params] )
参数 | 类型 | 描述 |
---|---|---|
method | 字符串 | 请求方法(例如 'POST' )。必须为大写。 |
url | 字符串 / HTTP URL | 请求 URL(例如 'http://example.com' )。 |
body(可选) | 字符串 / 对象 / ArrayBuffer | 请求正文;对象将被进行 x-www-form-urlencoded 编码。 |
params(可选) | 对象 | 参数 对象,包含额外请求参数。 |
返回值
类型 | 描述 |
---|---|
响应 | HTTP 响应 对象。 |
示例
使用 http.request() 发出 POST 请求
import http from 'k6/http';
const url = 'https://quickpizza.grafana.com/api/post';
export default function () {
const data = { name: 'Bert' };
// Using a JSON string as body
let res = http.request('POST', url, JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
});
console.log(res.json().name); // Bert
// Using an object as body, the headers will automatically include
// 'Content-Type: application/x-www-form-urlencoded'.
res = http.request('POST', url, data);
console.log(res.body); // name=Bert
}