grafana/k6 · error

getting response body

Error message

getting response body: %w

What it means

Wrap of Response.fetchBody in the public Body() accessor: after the redirect-status guard passes, the CDP body fetch (with its internal retries) still failed, and this wrap adds the "getting response body" context to the underlying error. Root causes are the same as the wrapped fetchBody failure — session closed, response evicted from the browser cache, or no body for the resource.

Solutions

  1. Read the body immediately after the response arrives rather than after later page activity
  2. Verify the browser session and page are still open at read time
  3. Retry the navigation/request if the failure is transient
  4. Inspect the wrapped error for the specific CDP reason (cache miss vs. closed session)
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/common/http.go:666 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/641f7631059d6bd3. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/http.go:665

		for name, values := range r.extraHeaders {
			headers[strings.ToLower(name)] = strings.Join(values, "\n")
		}
	} else {
		for name, values := range r.headers {
			headers[strings.ToLower(name)] = strings.Join(values, "\n")
		}
	}
	r.extraHeadersMu.RUnlock()
	return headers
}

// Body returns the response body as a bytes buffer.
func (r *Response) Body() ([]byte, error) {
	if r.status >= 300 && r.status <= 399 {
		return nil, fmt.Errorf("response body is unavailable for redirect responses")
	}
	if err := r.fetchBody(); err != nil {
		return nil, fmt.Errorf("getting response body: %w", err)
	}

	r.bodyMu.RLock()
	defer r.bodyMu.RUnlock()

	return r.body, nil
}

// bodySize returns the size in bytes of the response body.
func (r *Response) bodySize() int64 {
	// Skip redirect responses
	if r.status >= 300 && r.status <= 399 {
		return 0
	}

	if err := r.fetchBody(); err != nil {
		r.logger.Debugf("Response:bodySize:fetchBody",
			"url:%s method:%s err:%s", r.url, r.request.method, err)

View on GitHub (pinned to 8d06114777)