{"record":{"id":"7b9ace47086121f0","repo":"grafana/k6","slug":"parsing-http-credentials-w","errorCode":null,"errorMessage":"parsing HTTP credentials: %w","messagePattern":"parsing HTTP credentials: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/browser_context_mapping.go","lineNumber":106,"sourceCode":"\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn nil, bc.GrantPermissions(permissions, popts)\n\t\t\t}), nil\n\t\t},\n\t\t\"setDefaultNavigationTimeout\": bc.SetDefaultNavigationTimeout,\n\t\t\"setDefaultTimeout\":           bc.SetDefaultTimeout,\n\t\t\"setGeolocation\": func(geolocation sobek.Value) (*sobek.Promise, error) {\n\t\t\tgl, err := exportTo[common.Geolocation](vu.Runtime(), geolocation)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"parsing geo location: %w\", err)\n\t\t\t}\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn nil, bc.SetGeolocation(&gl)\n\t\t\t}), nil\n\t\t},\n\t\t\"setHTTPCredentials\": func(httpCredentials sobek.Value) (*sobek.Promise, error) {\n\t\t\tcreds, err := exportTo[common.Credentials](rt, httpCredentials)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"parsing HTTP credentials: %w\", err)\n\t\t\t}\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn nil, bc.SetHTTPCredentials(creds) //nolint:staticcheck\n\t\t\t}), nil\n\t\t},\n\t\t\"setOffline\": func(offline bool) *sobek.Promise {\n\t\t\treturn promise(vu, func() (any, error) {\n\t\t\t\treturn nil, bc.SetOffline(offline) //nolint:wrapcheck\n\t\t\t})\n\t\t},\n\t\t\"waitForEvent\": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) {\n\t\t\trt := vu.Runtime()\n\t\t\tctx := vu.Context()\n\n\t\t\tpopts, err := parseWaitForEventOptions(rt, optsOrPredicate, bc.Timeout())\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"parsing wait for event options: %w\", err)\n\t\t\t}","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/browser/browser_context_mapping.go#L88-L124","documentation":"browserContext.setHTTPCredentials(creds) exports its argument to common.Credentials, a struct of {username, password} strings. A non-object value or wrong-typed fields fail the export and are wrapped here. The API is deprecated (the mapping still exposes it for compatibility, marked with a staticcheck suppress) - prefer credentials on the HTTP layer instead.","triggerScenarios":"setHTTPCredentials('user:pass'); setHTTPCredentials({ user, pass }) (wrong key names); setHTTPCredentials({ username: 42 }); passing null/undefined where a partial object is expected to merge.","commonSituations":"Porting Playwright tests verbatim; not realizing the expected key names are username/password; scripted credential rotation passing joined strings.","solutions":["Pass exactly { username: 'user', password: 'pass' } with string values","Better: drop setHTTPCredentials (deprecated) and send an Authorization header or credentials in http requests directly","Double-check any object spread merging credentials with other settings"],"exampleFix":"// before: joined string / wrong keys\nawait context.setHTTPCredentials('user:pass');\nawait context.setHTTPCredentials({ user: 'u', pass: 'p' });\n\n// after: exact shape - or avoid the deprecated API via headers\nawait context.setHTTPCredentials({ username: 'user', password: 'pass' });\n// preferred: page.goto basic-auth URL or Authorization header per request","handlingStrategy":"type-guard","validationCode":"const creds = { username: String(process.env.USER_NAME ?? ''), password: String(process.env.PASSWORD ?? '') };\nif (typeof creds.username !== 'string' || typeof creds.password !== 'string') {\n  throw new TypeError('credentials must be { username: string, password: string }');\n}\n// better: skip the deprecated API and send an Authorization header per request","typeGuard":"function isCredentials(v) {\n  return (\n    typeof v === 'object' &&\n    v !== null &&\n    !Array.isArray(v) &&\n    typeof v.username === 'string' &&\n    typeof v.password === 'string'\n  );\n}","tryCatchPattern":"try {\n  await context.setHTTPCredentials(creds);\n} catch (e) {\n  if (String(e).includes('parsing HTTP credentials')) {\n    throw new Error('setHTTPCredentials expects { username, password } strings');\n  }\n  throw e;\n}","preventionTips":["Prefer Authorization headers or URL-embedded basic auth over the deprecated setHTTPCredentials","If you must use it, pass exactly { username, password } as strings"],"tags":["browser","browser-context","credentials","deprecated","options-parsing"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}