https://github.com/grafana/k6-jslib-httpx
httpx k6 library
https://github.com/grafana/k6-jslib-httpx
http jslib k6
Last synced: 25 days ago
JSON representation
httpx k6 library
- Host: GitHub
- URL: https://github.com/grafana/k6-jslib-httpx
- Owner: grafana
- License: apache-2.0
- Created: 2021-03-12T19:58:09.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-06-04T12:07:55.000Z (4 months ago)
- Last Synced: 2025-09-07T01:34:09.227Z (30 days ago)
- Topics: http, jslib, k6
- Language: JavaScript
- Homepage: https://grafana.com/docs/k6/latest/javascript-api/jslib/httpx/
- Size: 24.4 KB
- Stars: 13
- Watchers: 143
- Forks: 6
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# k6-jslib-httpx
httpx JavaScript libraryDocs: https://grafana.com/docs/k6/latest/javascript-api/jslib/httpx
Download the latest release from https://jslib.k6.io/
## Example
```javascript
import { test } from 'https://jslib.k6.io/functional/0.0.1/index.js';
import { Httpx } from 'https://jslib.k6.io/httpx/0.0.6/index.js';
import { randomIntBetween,
randomItem } 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';let 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() {
test(`Create a test user ${USERNAME}`, (t) => {
let resp = session.post(`/user/register/`, {
first_name: 'Crocodile',
last_name: 'Owner',
username: USERNAME,
password: PASSWORD,
});t.expect(resp.status).as("status").toEqual(201)
.and(resp).toHaveValidJson();
})&&
test(`Authenticate the new user ${USERNAME}`, (t) => {
let resp = session.post(`/auth/token/login/`, {
username: USERNAME,
password: PASSWORD
});t.expect(resp.status).as("Auth status").toBeBetween(200, 204)
.and(resp).toHaveValidJson()
.and(resp.json('access')).as("auth token").toBeTruthy();let authToken = resp.json('access');
// set the authorization header on the session for the subsequent requests.
session.addHeader('Authorization', `Bearer ${authToken}`);})
&&
test('Create a new crocodile', (t) => {
let payload = {
name: `Croc Name`,
sex: randomItem(["M", "F"]),
date_of_birth: '2019-01-01',
};let resp = session.post(`/my/crocodiles/`, payload);
t.expect(resp.status).as("Croc creation status").toEqual(201)
.and(resp).toHaveValidJson();
})}
```