{"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/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L304-L340","documentation":"Thrown by the deprecated SetHTTPCredentials: it stores credentials on the context and then calls updateHTTPCredentials (Network.setExtraHTTPHeaders) on every existing page; the page named by targetID failed. k6 also logs a deprecation warning because this API is scheduled for removal — the replacement is the httpCredentials option of browser.newContext().","triggerScenarios":"browserContext.setHTTPCredentials({ username, password }) while some page in the context has a dead CDP session, or after the browser connection dropped; calling it after pages were closed.","commonSituations":"Legacy scripts from before the httpCredentials option existed; basic-auth setups where credentials change mid-run; cleanup ordering where the call lands after teardown of pages.","solutions":["Replace setHTTPCredentials with browser.newContext({ httpCredentials: { username, password } }) — this is the supported path and applies before pages exist","If you must use the deprecated call, do it immediately after newContext() while no pages are open","Ensure pages in the context are alive when the call runs","Wrap in try/catch if it lives on a cleanup path"],"exampleFix":"// before\nconst ctx = browser.newContext();\nconst page = ctx.newPage();\nctx.setHTTPCredentials({ username: 'u', password: 'p' }); // deprecated, per-page update can fail\n\n// after\nconst ctx = browser.newContext({ httpCredentials: { username: 'u', password: 'p' } });\nconst page = ctx.newPage();","handlingStrategy":"fallback","validationCode":"// supported replacement: credentials via context options, applied before pages exist\nconst ctx = browser.newContext({\n  httpCredentials: { username: 'u', password: 'p' },\n});\nconst page = ctx.newPage();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Migrate off setHTTPCredentials — it is deprecated and will be removed","If credentials must change, create a new context with new httpCredentials instead of updating the old one","Never call deprecated per-page updaters when pages are closing"],"tags":["browser","cdp","http-auth","deprecated"],"backgroundTag":"cdp-protocol-error","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}