grafana/k6 · error

getting response body as text: %w

Error message

getting response body as text: %w

What it means

Response.Text() lazily fetches the body over CDP (same fetchBody path as Body/JSON, including the retry loop for late data) and returns it as a string. This error wraps that fetch failure; the bytes never arrived, so no string conversion is attempted.

Source

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

		Body:    r.bodySize(),
		Headers: r.headersSize(),
	}
}

// Status returns the response status code.
func (r *Response) Status() int64 {
	return r.status
}

// StatusText returns the response status text.
func (r *Response) StatusText() string {
	return r.statusText
}

// Text returns the response body as a string.
func (r *Response) Text() (string, error) {
	if err := r.fetchBody(); err != nil {
		return "", fmt.Errorf("getting response body as text: %w", err)
	}

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

	return string(r.body), nil
}

// URL returns the request URL.
func (r *Response) URL() string {
	return r.url
}

// Route allows to handle a request.
type Route struct {
	logger         *log.Logger
	networkManager *NetworkManager

View on GitHub (pinned to 93accf6570)

Solutions

  1. Call res.text() immediately after receiving the response, before any further navigation
  2. Skip body reads for requests you intercepted and fulfilled yourself
  3. Keep the page/context alive until text assertions complete
  4. Under intermittent failure, look at the wrapped CDP error in k6 logs to confirm the browser dropped

Example fix

// before
const res = await page.goto(url);
await fillManyForms();
const html = await res.text();

// after
const res = await page.goto(url);
const html = await res.text(); // capture immediately
await fillManyForms();
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const html = await res.text();
} catch (e) {
  if (!/getting response body as text/.test(String(e.message))) throw e;
  // body not retrievable: navigate back or re-issue the request
}

Prevention

When it happens

Trigger: Calling res.text() when Network.getResponseBody fails: page navigated away or closed first, response fulfilled via route.fulfill() with no stored data, cached/service-worker response without retrievable body, or the browser/session died.

Common situations: Grabbing page HTML for assertions after subsequent navigation; intercepted responses; reading bodies during teardown; browser instability under heavy parallel load.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/dd6bdce4982b3d21. Report an issue: GitHub.