Most k6 usage focuses on HTTP APIs, and the built-in http module makes it straightforward to send requests and validate responses. Checks, groups and custom metrics help you turn raw responses into meaningful test outcomes.
Making HTTP Calls and Using Checks
The http module supports common methods like GET, POST and PUT, while checks let you assert on status codes, bodies and custom conditions. You can group related requests under named groups to structure your scripts and reports.
import http from 'k6/http';
import { check, group, sleep } from 'k6';
export default function () {
group('browse products', () => {
const res = http.get('https://api.example.com/products');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 800ms': (r) => r.timings.duration < 800,
});
});
sleep(1);
}
checks metric, which you can also use in thresholds (for example requiring 99% of checks to pass).Effective use of checks ensures that your performance tests also verify functional behaviour under load.
Common Mistakes
Mistake 1 β Skipping checks to βfocus only on performanceβ
This hides failures.
β Wrong: Ignoring response status and content.
β Correct: Always assert basic correctness so fast errors do not appear as successes.
Mistake 2 β Writing a single giant script with no structure
This hurts maintainability.
β Wrong: Putting all logic in one long default function.
β Correct: Use groups and helper functions to organise flows.