Skip to main content
The k6/http module contains functionality for performing HTTP transactions. This is the primary module for load testing HTTP services.

Import

import http from 'k6/http';

HTTP Methods

get()

Issues an HTTP GET request.
url
string | HTTP URL
required
Request URL (e.g. http://example.com).
params
object
Params object containing additional request parameters like headers, tags, etc.
return
Response
HTTP Response object.
import http from 'k6/http';

export default function () {
  const res = http.get('https://test.k6.io');
  console.log(JSON.stringify(res.headers));
}

post()

Issues an HTTP POST request.
url
string | HTTP URL
required
Request URL (e.g. http://example.com).
body
string | object | ArrayBuffer
Request body. Objects will be automatically x-www-form-urlencoded.
params
object
Params object containing additional request parameters.
return
Response
HTTP Response object.
import http from 'k6/http';

const url = 'https://quickpizza.grafana.com/api/json';

export default function () {
  let data = { name: 'Bert' };

  // Using a JSON string as body
  let res = http.post(url, JSON.stringify(data), {
    headers: { 'Content-Type': 'application/json' },
  });
  console.log(res.json().json.name); // Bert
}

put()

Issues an HTTP PUT request.
url
string | HTTP URL
required
Request URL (e.g. http://example.com).
body
string | object | ArrayBuffer
Request body. Objects will be x-www-form-urlencoded.
params
object
Params object containing additional request parameters.
return
Response
HTTP Response object.
import http from 'k6/http';

const url = 'https://quickpizza.grafana.com/api/put';

export default function () {
  const headers = { 'Content-Type': 'application/json' };
  const data = { name: 'Bert' };

  const res = http.put(url, JSON.stringify(data), { headers: headers });

  console.log(JSON.parse(res.body).name);
}

del()

Issues an HTTP DELETE request.
url
string | HTTP URL
required
Request URL (e.g. http://example.com).
body
string | object | ArrayBuffer
Request body (optional, discouraged). Sending a DELETE request with a body has no defined semantics and may cause some servers to reject it.
params
object
Params object containing additional request parameters.
return
Response
HTTP Response object.
import http from 'k6/http';

const url = 'https://quickpizza.grafana.com/api/delete';

export default function () {
  const params = { headers: { 'X-MyHeader': 'k6test' } };
  http.del(url, null, params);
}

request()

Issues any type of HTTP request with a custom method.
method
string
required
Request method (e.g. 'POST'). Must be uppercase.
url
string | HTTP URL
required
Request URL (e.g. 'http://example.com').
body
string | object | ArrayBuffer
Request body. Objects will be x-www-form-urlencoded encoded.
params
object
Params object containing additional request parameters.
return
Response
HTTP Response object.
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
  res = http.request('POST', url, data);
  console.log(res.body); // name=Bert
}

batch()

Issues multiple HTTP requests in parallel (like browsers do).
requests
array | object
required
An array or object containing requests, in string or object form.
return
object | array
Returns Response objects. It’s an array when you pass an array as requests, and an object with string keys when you use named requests.
To set batch size, use the batchPerHost option in your test configuration.
import { check } from 'k6';
import http from 'k6/http';

export default function () {
  const responses = http.batch(['http://test.k6.io', 'http://test.k6.io/pi.php']);

  check(responses[0], {
    'main page 200': (res) => res.status === 200,
  });

  check(responses[1], {
    'pi page 200': (res) => res.status === 200,
    'pi page has right content': (res) => res.body === '3.14',
  });
}
When using arrays to define requests, use the specific order: [method, url, body, params].

Build docs developers (and LLMs) love