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.

Form-encoded request bodies

When you pass a JavaScript object as the body without file uploads, k6 encodes it as application/x-www-form-urlencoded:

  • null and undefined become empty values, such as name=. The field remains in the request.
  • Arrays produce repeated keys, such as tag=one&tag=two.
  • Nested objects and nested arrays become empty values and cause k6 to log a warning. This also applies to objects inside arrays.

For example, { name: null, tags: ['one', null] } becomes name=&tags=one&tags=. To preserve nested data, serialize the body with JSON.stringify() and set the Content-Type header to application/json. Setting the header alone does not convert an object body to JSON.

These rules also apply to methods such as post().

Returns

TypeDescription
ResponseHTTP Response object.

Example

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

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
}