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 RSS

httpx

The httpx module is an external JavaScript library that wraps around the native k6/http module. It’s a http client with features that are not yet available in the native module.

  • ability to set http options globally (such as timeout)
  • ability to set default tags and headers that will be used for all requests
  • more user-friendly arguments to request functions (get, post, put take the same arguments)

httpx module integrates well with the expect library.

The source code is on GitHub. Please request features and report bugs through GitHub issues.

Caution

This library is in active development.

This library is stable enough to be useful, but pay attention to the new versions released on jslib.k6.io.

This documentation is for the only last version only. If you discover that some of the following doesn’t work, you might be using an older version.

Methods

FunctionDescription
asyncRequest(method, url, [body], [params])Generic method for making arbitrary, asynchronous HTTP requests.
request(method, url, [body], [params])Generic method for making arbitrary HTTP requests.
get(url, [body], [params])Makes GET request
post(url, [body], [params])Makes POST request
put(url, [body], [params])Makes PUT request
patch(url, [body], [params])Makes PATCH request
delete(url, [body], [params])Makes DELETE request
batch(requests)Batches multiple HTTP requests together to issue them in parallel.
setBaseUrl(url)Sets the base URL for the session
addHeader(key, value)Adds a header to the session
addHeaders(object)Adds multiple headers to the session
clearHeader(name)Removes header from the session
addTag(key, value)Adds a tag to the session
addTags(object)Adds multiple tags to the session
clearTag(name)Removes tag from the session

Example

JavaScript
import { fail } from 'k6';
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

const USERNAME = `user${randomIntBetween(1, 100000)}@example.com`; // random email address
const PASSWORD = 'superCroc2021';

const session = new Httpx({
  baseURL: 'https://test-api.k6.io',
  headers: {
    'User-Agent': 'My custom user agent',
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  timeout: 20000, // 20s timeout.
});

export default function testSuite() {
  const registrationResp = session.post(`/user/register/`, {
    first_name: 'Crocodile',
    last_name: 'Owner',
    username: USERNAME,
    password: PASSWORD,
  });

  if (registrationResp.status !== 201) {
    fail('registration failed');
  }

  const loginResp = session.post(`/auth/token/login/`, {
    username: USERNAME,
    password: PASSWORD,
  });

  if (loginResp.status !== 200) {
    fail('Authentication failed');
  }

  const authToken = loginResp.json('access');

  // set the authorization header on the session for the subsequent requests.
  session.addHeader('Authorization', `Bearer ${authToken}`);

  const payload = {
    name: `Croc Name`,
    sex: 'M',
    date_of_birth: '2019-01-01',
  };

  // this request uses the Authorization header set above.
  const respCreateCrocodile = session.post(`/my/crocodiles/`, payload);

  if (respCreateCrocodile.status !== 201) {
    fail('Crocodile creation failed');
  } else {
    console.log('New crocodile created');
  }
}