Menu
Open source
Params
Params is an object used by the http.* methods that generate HTTP requests. Params contains request-specific options like e.g. HTTP headers that should be inserted into the request.
Example of custom HTTP headers and tags
A k6 script that will make an HTTP request with a custom HTTP header and tag results data with a specific tag
import http from 'k6/http';
export default function () {
const params = {
cookies: { my_cookie: 'value' },
headers: { 'X-MyHeader': 'k6test' },
redirects: 5,
tags: { k6test: 'yes' },
};
const res = http.get('https://k6.io', params);
}Example using http.batch() with Params
Here is another example using
http.batch() with a Params argument:
import http from 'k6/http';
const url1 = 'https://api.k6.io/v3/account/me';
const url2 = 'https://httpbin.test.k6.io/get';
const apiToken = 'f232831bda15dd233c53b9c548732c0197619a3d3c451134d9abded7eb5bb195';
const requestHeaders = {
'User-Agent': 'k6',
'Authorization': 'Token ' + apiToken,
};
export default function () {
const res = http.batch([
{ method: 'GET', url: url1, params: { headers: requestHeaders } },
{ method: 'GET', url: url2 },
]);
}Example of Digest Authentication
Here is one example of how to use the Params to Digest Authentication.
import http from 'k6/http';
import { check } from 'k6';
export default function () {
// Passing username and password as part of URL plus the auth option will authenticate using HTTP Digest authentication
const res = http.get('http://user:passwd@httpbin.test.k6.io/digest-auth/auth/user/passwd', {
auth: 'digest',
});
// Verify response
check(res, {
'status is 200': (r) => r.status === 200,
'is authenticated': (r) => r.json().authenticated === true,
'is correct user': (r) => r.json().user === 'user',
});
}Example of overriding discardResponseBodies
import http from 'k6/http';
export const options = { discardResponseBodies: true };
export default function () {}
export function setup() {
// Get 10 random bytes as an ArrayBuffer. Without the responseType the body
// will be null.
const response = http.get('https://httpbin.test.k6.io/bytes/10', {
responseType: 'binary',
});
// response.body is an ArrayBuffer, so wrap it in a typed array view to access
// its elements.
const bodyView = new Uint8Array(response.body);
// This will output something like `176,61,15,66,233,98,223,196,43,1`
console.log(bodyView);
}