k6/http module provides methods for all standard HTTP operations.
Making HTTP requests
The simplest HTTP request is a GET request:POST request with payload
Here’s a more complex example showing a POST request with JSON payload:Available HTTP methods
Thek6/http module supports all standard HTTP methods:
| Method | Description |
|---|---|
http.get() | Issue an HTTP GET request |
http.post() | Issue an HTTP POST request |
http.put() | Issue an HTTP PUT request |
http.patch() | Issue an HTTP PATCH request |
http.del() | Issue an HTTP DELETE request |
http.head() | Issue an HTTP HEAD request |
http.options() | Issue an HTTP OPTIONS request |
http.request() | Issue any type of HTTP request |
http.batch() | Issue multiple HTTP requests in parallel |
Batch requests
Usehttp.batch() to send multiple requests in parallel, similar to how browsers load resources:
HTTP request parameters
The third parameter to HTTP methods accepts an object with request configuration:Following redirects
By default, k6 automatically follows redirects. You can customize this behavior:- Global setting
- Per-request setting
Set
maxRedirects in options to configure redirect behavior for all requests:HTTP request tags
k6 automatically tags HTTP requests with metadata for filtering and analysis:| Tag | Description | Example |
|---|---|---|
name | URL or custom name | http://test.k6.io |
method | HTTP method | GET, POST |
status | Response status code | 200, 404 |
url | Request URL | http://test.k6.io |
group | Group name (if in a group) | ::user login |
scenario | Scenario name | default |
expected_response | Whether response was expected | true, false |
Example tagged output
Here’s how k6 stores metrics with tags:URL grouping
When testing dynamic URLs, you’ll want to group them to prevent metrics fragmentation.The problem: dynamic URLs
This code creates 100 different metrics, one for each unique URL:Solution 1: Custom name tag
Set a customname tag to group dynamic URLs:
Solution 2: http.url template
Use thehttp.url template literal wrapper:
${}.
Response object
HTTP methods return a Response object with request/response details:Useful response properties
res.status
HTTP status code (200, 404, etc.)
res.body
Response body as string
res.json()
Parse response body as JSON
res.timings
Detailed timing breakdown
res.headers
Response headers
res.cookies
Response cookies
Complete example
Here’s a comprehensive example combining multiple concepts:Best practices
Group dynamic URLs
Use name tags or http.url to prevent metric fragmentation with dynamic URLs.
Use meaningful names
Tag requests with descriptive names for easier analysis.
Check responses
Always validate critical responses with checks.
Batch when possible
Use http.batch() to simulate realistic browser behavior.