Menu

Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.

Documentationbreadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/httpbreadcrumb arrow request( method, url, [body], [params] )
Open source

request( method, url, [body], [params] )

ParameterTypeDescription
methodstringRequest method (e.g. 'POST'). Must be uppercase.
urlstring /HTTP URLRequest URL (e.g. 'http://example.com').
body (optional)string / object / ArrayBufferRequest body; Objects will be x-www-form-urlencoded encoded.
params (optional)objectParams object containing additional request parameters.

Returns

TypeDescription
ResponseHTTP Response object.

Example

Using http.request() to issue a POST request:

JavaScript
import http from 'k6/http';

const url = 'https://httpbin.test.k6.io/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().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.json().form.name); // Bert
}