{"record":{"id":"dd6bdce4982b3d21","repo":"grafana/k6","slug":"getting-response-body-as-text-w","errorCode":null,"errorMessage":"getting response body as text: %w","messagePattern":"getting response body as text: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/http.go","lineNumber":821,"sourceCode":"\t\tBody:    r.bodySize(),\n\t\tHeaders: r.headersSize(),\n\t}\n}\n\n// Status returns the response status code.\nfunc (r *Response) Status() int64 {\n\treturn r.status\n}\n\n// StatusText returns the response status text.\nfunc (r *Response) StatusText() string {\n\treturn r.statusText\n}\n\n// Text returns the response body as a string.\nfunc (r *Response) Text() (string, error) {\n\tif err := r.fetchBody(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"getting response body as text: %w\", err)\n\t}\n\n\tr.bodyMu.RLock()\n\tdefer r.bodyMu.RUnlock()\n\n\treturn string(r.body), nil\n}\n\n// URL returns the request URL.\nfunc (r *Response) URL() string {\n\treturn r.url\n}\n\n// Route allows to handle a request.\ntype Route struct {\n\tlogger         *log.Logger\n\tnetworkManager *NetworkManager\n","sourceCodeStart":803,"sourceCodeEnd":839,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/http.go#L803-L839","documentation":"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.","triggerScenarios":"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.","commonSituations":"Grabbing page HTML for assertions after subsequent navigation; intercepted responses; reading bodies during teardown; browser instability under heavy parallel load.","solutions":["Call res.text() immediately after receiving the response, before any further navigation","Skip body reads for requests you intercepted and fulfilled yourself","Keep the page/context alive until text assertions complete","Under intermittent failure, look at the wrapped CDP error in k6 logs to confirm the browser dropped"],"exampleFix":"// before\nconst res = await page.goto(url);\nawait fillManyForms();\nconst html = await res.text();\n\n// after\nconst res = await page.goto(url);\nconst html = await res.text(); // capture immediately\nawait fillManyForms();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const html = await res.text();\n} catch (e) {\n  if (!/getting response body as text/.test(String(e.message))) throw e;\n  // body not retrievable: navigate back or re-issue the request\n}","preventionTips":["Capture text bodies immediately after the response","Do not read bodies after page navigation or during teardown","For intercepted requests, use the payload you supplied"],"tags":["browser","network","response-body","text","cdp"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}