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.

Open source

HTTP Requests

When you create a new load test, one of the first steps is to define the HTTP requests that you would like to test.

Make HTTP Requests

A GET request looks like this:

JavaScript
import http from 'k6/http';

export default function () {
  http.get('http://test.k6.io');
}

For something slightly more complex, this example shows a POST request with an email/password authentication payload:

JavaScript
import http from 'k6/http';

export default function () {
  const url = 'http://test.k6.io/login';
  const payload = JSON.stringify({
    email: 'aaa',
    password: 'bbb',
  });

  const params = {
    headers: {
      'Content-Type': 'application/json',
    },
  };

  http.post(url, payload, params);
}

Available methods

The http module handles all kinds of HTTP requests and methods.

NameValue
batch()Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).
del()Issue an HTTP DELETE request.
get()Issue an HTTP GET request.
head()Issue an HTTP HEAD request.
options()Issue an HTTP OPTIONS request.
patch()Issue an HTTP PATCH request.
post()Issue an HTTP POST request.
put()Issue an HTTP PUT request.
request()Issue any type of HTTP request.

HTTP Request Tags

k6 automatically applies tags to your HTTP requests. You can use these tags to filter your results and organize your analysis.

NameDescription
expected_responseBy default, response statuses between 200 and 399 are true. Change the default behavior with setResponseCallback.
groupWhen the request runs inside a group, the tag value is the group name. Default is empty.
nameDefaults to URL requested
methodRequest method (GET, POST, PUT etc.)
scenarioWhen the request runs inside a scenario, the tag value is the scenario name. Default is default.
statusresponse status
urldefaults to URL requested

The following snippet shows a JSON example of how a test-result data point is logged. In this example, the metric is the duration of an HTTP request.

Note how the tags object groups data.

json
{
  "type": "Point",
  "metric": "http_req_duration",
  "data": {
    "time": "2017-06-02T23:10:29.52444541+02:00",
    "value": 586.831127,
    "tags": {
      "expected_response": "true",
      "group": "",
      "method": "GET",
      "name": "http://test.k6.io",
      "scenario": "default",
      "status": "200",
      "url": "http://test.k6.io"
    }
  }
}

Group URLs under one tag

By default, tags have a name field that holds the value of the request URL. If your test has dynamic URL paths, you might not want this behavior, which could bring a large number of unique URLs into the metrics stream. For example, the following code accesses 100 different URLs:

JavaScript
import http from 'k6/http';

export default function () {
  for (let id = 1; id <= 100; id++) {
    http.get(`http://example.com/posts/${id}`);
  }
}
// tags.name=\"http://example.com/posts/1\",
// tags.name=\"http://example.com/posts/2\",

You might prefer to report this data in a single metric: To aggregate data from dynamic URLs, explicitly set a name tag:

JavaScript
import http from 'k6/http';

export default function () {
  for (let id = 1; id <= 100; id++) {
    http.get(`http://example.com/posts/${id}`, {
      tags: { name: 'PostsItemURL' },
    });
  }
}
// tags.name=\"PostsItemURL\",
// tags.name=\"PostsItemURL\",

That code would produce JSON output like this:

json
// For http://example.com/1, note that the url is not present in the JSON.

{
    "type":"Point",
    "metric":"http_req_duration",
    "data": {
        "time":"2017-06-02T23:10:29.52444541+02:00",
        "value":586.831127,
        "tags": {
            "method":"GET",
            "name":"PostsItemURL",
            "status":"200",
            "url":"PostsItemURL"
        }
    }
}

// and for http://example.com/2

{
    "type":"Point",
    "metric":"http_req_duration",
    "data": {
        "time":"2017-06-02T23:10:29.58582529+02:00",
        "value":580.839273,
        "tags": {
            "method":"GET",
            "name":"PostsItemURL",
            "status":"200",
            "url":"PostsItemURL"
        }
    }
}

Note how these two objects have the same name, despite having different URLs. If you filter the results for the tag name: PostsItemURL, the results include all data points from all 100 URLs.

As an alternative, you can also use the http.url wrapper to set the name tag with a string template value:

JavaScript
import http from 'k6/http';

export default function () {
  for (let id = 1; id <= 100; id++) {
    http.get(http.url`http://example.com/posts/${id}`);
  }
}
// tags.name="http://example.com/posts/${}",
// tags.name="http://example.com/posts/${}",