grafana/k6 · error
unexpected HTTP error from
Error message
unexpected HTTP error from %s: %d %s
What it means
CheckResponse got a non-2xx response whose body could not be unmarshalled as the API's JSON error payload, and httperr.ClassifyStatus had no mapping for that status code. The fallback error names the requesting URL, numeric status, and status text so you can see which endpoint failed and how — but the server's human-readable reason (usually HTML or empty) is lost.
Solutions
- Retry after a short delay — gateway/proxy errors are frequently transient; the SDK's built-in retry only covers some paths
- Reproduce outside k6 (curl -v the same URL with your token) to see the raw body the gateway returns
- If a proxy is the source, bypass it for the cloud host or fix its error pages / allowlisting
- If it persists, capture r.Request.URL and status from the message and report to Grafana Cloud support with the timestamp
Example fix
// before
err := client.StartTest(ctx, id)
// message: unexpected HTTP error from https://...: 502 Bad Gateway
// after
// wrap and preserve the body for diagnosis
dec, _ := io.ReadAll(resp.Body)
return fmt.Errorf("%w; body preview: %.200s", err, string(dec)) Defensive patterns
Strategy: try-catch
Type guard
func isResponseError(err error) (*cloudapiv6.ResponseError, bool) {
var re *cloudapiv6.ResponseError
if errors.As(err, &re) { return re, true }
return nil, false
} Try / catch
err := client.StartTest(ctx, id)
if err != nil {
if _, ok := isResponseError(err); !ok && strings.Contains(err.Error(), "unexpected HTTP error") {
// non-JSON gateway body: almost always transient proxy/LB trouble
time.Sleep(10 * time.Second)
err = client.StartTest(ctx, id)
}
if err != nil { return fmt.Errorf("starting test: %w", err) }
} Prevention
- Expect HTML/empty bodies from proxies; retry gateway-class statuses before escalating
- Capture the raw body in debug builds to identify which middlebox produced it
- Keep cloud hosts on your proxy allowlist so error pages never originate in-network
When it happens
Trigger: A reverse proxy, load balancer, or captive portal returning an HTML error page (502/503/499), an empty body with an unusual status (520-527 from Cloudflare), or any non-JSON body for a status ClassifyStatus does not special-case.
Common situations: Corporate proxies intercepting requests to the cloud host, transient gateway errors during Grafana Cloud incidents, self-hosted misconfigured gateways, or WAFs blocking the k6 user-agent with a 406/418 HTML page.
Related errors
- notify test run completed
- an error occurred communicating with k6 Cloud
- create or find load test
- default stack configured but the default project ID is not…
- invalid stack URL
AI-assisted analysis of grafana/k6@8d06114777 (2026-08-31).
Data as JSON: /api/errors/21bf1b2f5e8bafa1.
Report an issue: GitHub.
Appendix: source
Thrown at internal/cloudapi/v6/client.go:91
return errUnknown
}
if c := r.StatusCode; c >= 200 && c <= 299 {
return nil
}
data, err := io.ReadAll(r.Body)
if err != nil {
return err
}
var respErr error
var payload ResponseError
if err := json.Unmarshal(data, &payload); err != nil {
if classified := httperr.ClassifyStatus(r.StatusCode); classified != nil {
respErr = classified
} else {
respErr = fmt.Errorf(
"unexpected HTTP error from %s: %d %s",
r.Request.URL,
r.StatusCode,
http.StatusText(r.StatusCode),
)
}
} else {
payload.Response = r
respErr = payload
}
if r.StatusCode == http.StatusUnauthorized {
return errext.WithHint(respErr,
"Authenticate by running `k6 cloud login` "+
"or verify the active Grafana Cloud token (K6_CLOUD_TOKEN, options.cloud.token)")
}
return respErr
}View on GitHub (pinned to 8d06114777)