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

  1. Retry after a short delay — gateway/proxy errors are frequently transient; the SDK's built-in retry only covers some paths
  2. Reproduce outside k6 (curl -v the same URL with your token) to see the raw body the gateway returns
  3. If a proxy is the source, bypass it for the cloud host or fix its error pages / allowlisting
  4. 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

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


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)