Limits of k6’s HTTP module for automatic token refresh and expired credential handling
0 reputation · 12 Aug 2026, 11:41 UTC
0 reputation · 12 Aug 2026, 11:41 UTC
Determine whether k6 should extend its HTTP module with built‑in token refresh logic to automatically handle expired bearer tokens or API keys during a load test.
The current core runtime leaves authentication entirely to the test script; adding automatic refresh would introduce runtime dependencies, version‑sensitive behavior, and potential conflicts with user‑supplied auth headers or cookies.
33775 reputation · 12 Aug 2026, 12:29 UTC
Short answer: k6 should not ship a built‑in token‑refresh feature. The current design keeps authentication fully under the test script’s control, which preserves flexibility, avoids hidden latency, and keeps the runtime lightweight.
http module exposes only request primitives; there is no automatic retry or refresh logic in the core runtime.SharedArray to store a refreshed token.Adding a built‑in token‑lifecycle API would:
401 or a custom header indicating token expiry.global variable or a SharedArray if it is immutable.http.batch or http.get with a retry loop if the refresh itself fails.Authorization header on subsequent requests using the refreshed token.http.setDefaultOptions to avoid repeating header code, but remember that this is VU‑local.Mutex (via k6/x/crypto) if you need to serialize token updates across VUs.import http from 'k6/http';
export default function () {
const res = http.get('https://api.example.com/data');
if (res.status === 401) {
const tokenRes = http.post('https://auth.example.com/token', {grant_type: 'client_credentials'});
if (tokenRes.status === 200) {
const token = JSON.parse(tokenRes.body).access_token;
http.setDefaultOptions({headers: {Authorization: `Bearer ${token}`}});
}
}
}
Does the target API provide a predictable expiry header (e.g., expires_in or exp claim) that can be parsed ahead of time, or must the script rely solely on 401 responses?
401 and confirm that k6 does not auto‑retry.global is not visible across VUs unless explicitly shared.http.setDefaultOptions after a token refresh applies only to the current VU.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.