{"record":{"id":"874763afcceddab7","repo":"grafana/k6","slug":"setting-http-credentials-in-target-id-s-w","errorCode":null,"errorMessage":"setting HTTP credentials in target ID %s: %w","messagePattern":"setting HTTP credentials in target ID (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":322,"sourceCode":"\n\treturn nil\n}\n\n// SetHTTPCredentials sets username/password credentials to use for HTTP authentication.\n//\n// Deprecated: Create a new BrowserContext with httpCredentials instead.\n// See for details:\n// - https://github.com/microsoft/playwright/issues/2196#issuecomment-627134837\n// - https://github.com/microsoft/playwright/pull/2763\nfunc (b *BrowserContext) SetHTTPCredentials(hc Credentials) error {\n\tb.logger.Warnf(\"setHTTPCredentials\", \"setHTTPCredentials is deprecated.\"+\n\t\t\" Create a new BrowserContext with httpCredentials instead.\")\n\tb.logger.Debugf(\"BrowserContext:SetHTTPCredentials\", \"bctxid:%v\", b.id)\n\n\tb.opts.HTTPCredentials = hc\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.updateHTTPCredentials(); err != nil {\n\t\t\treturn fmt.Errorf(\"setting HTTP credentials in target ID %s: %w\", p.targetID, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// SetOffline toggles the browser's connectivity on/off.\nfunc (b *BrowserContext) SetOffline(offline bool) error {\n\tb.logger.Debugf(\"BrowserContext:SetOffline\", \"bctxid:%v offline:%t\", b.id, offline)\n\n\tb.opts.Offline = offline\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.updateOffline(); err != nil {\n\t\t\treturn fmt.Errorf(\n\t\t\t\t\"setting offline status to %t for the browser context ID %s: %w\",\n\t\t\t\toffline, b.id, err,\n\t\t\t)\n\t\t}","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L304-L340","documentation":"Thrown by the deprecated BrowserContext.SetHTTPCredentials when it fails to push new HTTP credentials to one of the open pages in the context. After storing the credentials in the context options, the method iterates every page and calls updateHTTPCredentials, which sends a CDP emulation command to each frame session; if that CDP call fails, the error is wrapped with the failing page's target ID. The method itself is deprecated because Playwright removed it: credentials are now fixed at context creation.","triggerScenarios":"Calling browserContext.setHTTPCredentials({username, password}) on a context whose pages/frame sessions have a broken or closed CDP connection (browser crashed, page target closed mid-update, browser disconnected). The error can also surface when the k6 browser process is being torn down while the call is in flight.","commonSituations":"Scripts ported from Puppeteer or older k6 browser versions that set credentials after context creation; calling setHTTPCredentials after the browser or one of its pages has already closed; flaky headless Chrome environments (memory pressure, container OOM kills) that drop the CDP websocket.","solutions":["Replace the deprecated call: close the context and create a new one with browser.newContext({ httpCredentials: { username, password } }) (or launchPersistentContext), which is the supported approach.","If you must keep the call, verify the browser connection and pages are alive first (no prior crash in logs) and that the error is not caused by an already-closed target.","Check the wrapped error (%w) in the message to see which target ID failed and inspect k6 debug logs (--log-output=debug or DEBUG=true) for the underlying CDP failure."],"exampleFix":"// before\nconst context = browser.newContext();\ncontext.setHTTPCredentials({ username: 'user', password: 'pass' });\n\n// after\nconst context = browser.newContext({\n  httpCredentials: { username: 'user', password: 'pass' },\n});","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  context.setHTTPCredentials({ username, password });\n} catch (e) {\n  // Prefer migrating off the deprecated API instead of handling this.\n  console.error(`setHTTPCredentials failed (target closed?): ${e.message}`);\n}","preventionTips":["Create the browser context with httpCredentials in newContext()/launchPersistentContext() instead of calling the deprecated setHTTPCredentials.","Avoid calling context-level mutators after closing pages or during teardown.","Watch k6 deprecation warnings in logs — the Warnf('setHTTPCredentials is deprecated') line flags this API for removal."],"tags":["http","authentication","cdp","deprecated","browser-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}