菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/httpbreadcrumb arrow 请求( method, url, [body], [params] )
开源

请求( method, url, [body], [params] )

参数类型描述
method字符串请求方法(例如 'POST')。必须为大写。
url字符串 / HTTP URL请求 URL(例如 'http://example.com')。
body(可选)字符串 / 对象 / ArrayBuffer请求正文;对象将被进行 x-www-form-urlencoded 编码。
params(可选)对象参数 对象,包含额外请求参数。

返回值

类型描述
响应HTTP 响应 对象。

示例

使用 http.request() 发出 POST 请求

JavaScript
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
}