No results for

Powered byAlgolia

HTTP Requests

When testers create new load test, the first step is often to define the HTTP requests to test the system with.

Make HTTP Requests

A GET request looks like this:

http_get.js
1import http from 'k6/http';
2
3export default function () {
4 http.get('http://test.k6.io');
5}

For something slightly more complex, this POST request authenticates on a service or site:

http_post.js
1import http from 'k6/http';
2
3export default function () {
4 const url = 'http://test.k6.io/login';
5 const payload = JSON.stringify({
6 email: 'aaa',
7 password: 'bbb',
8 });
9
10 const params = {
11 headers: {
12 'Content-Type': 'application/json',
13 },
14 };
15
16 http.post(url, payload, params);
17}

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.

data_point.json
1{
2 "type": "Point",
3 "metric": "http_req_duration",
4 "data": {
5 "time": "2017-06-02T23:10:29.52444541+02:00",
6 "value": 586.831127,
7 "tags": {
8 "expected_response": "true",
9 "group": "",
10 "method": "GET",
11 "name": "http://test.k6.io",
12 "scenario": "default",
13 "status": "200",
14 "url": "http://test.k6.io"
15 }
16 }
17}

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:

grouping.js
1import http from 'k6/http';
2
3export default function () {
4 for (let id = 1; id <= 100; id++) {
5 http.get(`http://example.com/posts/${id}`);
6 }
7}
8// tags.name=\"http://example.com/posts/1\",
9// 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:

explicit_tag.js
1import http from 'k6/http';
2
3export default function () {
4 for (let id = 1; id <= 100; id++) {
5 http.get(`http://example.com/posts/${id}`, {
6 tags: { name: 'PostsItemURL' },
7 });
8 }
9}
10// tags.name=\"PostsItemURL\",
11// tags.name=\"PostsItemURL\",

That code would produce JSON output like this:

1{
2 "type":"Point",
3 "metric":"http_req_duration",
4 "data": {
5 "time":"2017-06-02T23:10:29.52444541+02:00",
6 "value":586.831127,
7 "tags": {
8 "method":"GET",
9 "name":"PostsItemURL",
10 "status":"200",
11 "url":"http://example.com/1"
12 }
13 }
14}
15
16// and
17
18{
19 "type":"Point",
20 "metric":"http_req_duration",
21 "data": {
22 "time":"2017-06-02T23:10:29.58582529+02:00",
23 "value":580.839273,
24 "tags": {
25 "method":"GET",
26 "name":"PostsItemURL",
27 "status":"200",
28 "url":"http://example.com/2"
29 }
30 }
31}

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:

1import http from 'k6/http';
2
3export default function () {
4 for (let id = 1; id <= 100; id++) {
5 http.get(http.url`http://example.com/posts/${id}`);
6 }
7}
8// tags.name="http://example.com/posts/${}",
9// tags.name="http://example.com/posts/${}",