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 containing additional request parameters like headers, tags, etc.
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 containing additional request parameters.
JSON body
Form data
Binary data
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 containing additional request parameters.
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 containing additional request parameters.
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.
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 containing additional request parameters.
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).
An array or object containing requests, in string or object form.
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.
Array of strings
Array of arrays
Array of objects
Named requests
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' ,
});
}
import http from 'k6/http' ;
import { check } from 'k6' ;
export default function () {
const responses = http . batch ([
[ 'GET' , 'https://test.k6.io' , null , { tags: { ctype: 'html' } }],
[ 'GET' , 'https://test.k6.io/style.css' , null , { tags: { ctype: 'css' } }],
[ 'GET' , 'https://test.k6.io/images/logo.png' , null , { tags: { ctype: 'images' } }],
]);
check ( responses [ 0 ], {
'main page status was 200' : ( res ) => res . status === 200 ,
});
}
import http from 'k6/http' ;
import { check } from 'k6' ;
export default function () {
const req1 = {
method: 'GET' ,
url: 'https://quickpizza.grafana.com/api/get' ,
};
const req2 = {
method: 'DELETE' ,
url: 'https://quickpizza.grafana.com/api/delete' ,
};
const req3 = {
method: 'POST' ,
url: 'https://quickpizza.grafana.com/api/post' ,
body: JSON . stringify ({ hello: 'world!' }),
params: {
headers: { 'Content-Type' : 'application/json' },
},
};
const responses = http . batch ([ req1 , req2 , req3 ]);
check ( responses [ 2 ], {
'form data OK' : ( res ) => JSON . parse ( res . body )[ 'hello' ] == 'world!' ,
});
}
import http from 'k6/http' ;
import { check } from 'k6' ;
export default function () {
const requests = {
'front page' : 'https://k6.io' ,
'features page' : {
method: 'GET' ,
url: 'https://k6.io/features' ,
params: { headers: { 'User-Agent' : 'k6' } },
},
};
const responses = http . batch ( requests );
// Access results using the request name as index
check ( responses [ 'front page' ], {
'front page status was 200' : ( res ) => res . status === 200 ,
});
}
When using arrays to define requests, use the specific order: [method, url, body, params].