{"record":{"id":"00fca6557742beaa","repo":"apify/crawlee","slug":"could-not-parse-cookie-header-string-cookiehead","errorCode":null,"errorMessage":"Could not parse cookie header string: ${cookieHeaderString}","messagePattern":"Could not parse cookie header string: (.+?)","errorType":"exception","errorClass":"CookieParseError","httpStatus":null,"severity":"error","filePath":"packages/core/src/cookie_utils.ts","lineNumber":22,"sourceCode":"import { serviceLocator } from './service_locator.js';\nimport { CookieParseError } from './session_pool/errors.js';\n\nexport interface ResponseLike {\n    url?: string | (() => string);\n    headers?: Record<string, string | string[] | undefined> | (() => Record<string, string | string[] | undefined>);\n}\n\n/**\n * @internal\n */\nexport function getCookiesFromResponse(response: Response): Cookie[] {\n    const headers = response.headers;\n    const cookieHeaders = headers.getSetCookie();\n\n    try {\n        return cookieHeaders.map((cookie) => Cookie.parse(cookie)!);\n    } catch (e) {\n        throw new CookieParseError(cookieHeaders);\n    }\n}\n\n/**\n * Calculate cookie expiration date\n * @param maxAgeSecs\n * @returns Calculated date by session max age seconds.\n * @internal\n */\nexport function getDefaultCookieExpirationDate(maxAgeSecs: number) {\n    return new Date(Date.now() + maxAgeSecs * 1000);\n}\n\n/**\n * Transforms tough-cookie to puppeteer cookie.\n * @param toughCookie Cookie from CookieJar\n * @return Cookie compatible with browser pool\n * @internal","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/cookie_utils.ts#L4-L40","documentation":"This library parses Set-Cookie headers from an HTTP response into cookie objects using a cookie parser. When parsing any cookie header string fails (malformed syntax), the library wraps the failure in a CookieParseError carrying the raw header strings. It is thrown from getCookiesFromResponse, used by response processing and the cookies() helper.","triggerScenarios":"Calling processHttpResponse or cookies() on a Response whose headers.getSetCookie() returns one or more malformed cookie strings that Cookie.parse cannot parse.","commonSituations":"Scraping servers or proxies emitting non-standard Set-Cookie values (broken quoting, invalid dates, control characters); intercepting/replaying responses with hand-crafted headers; proxy middleware corrupting headers.","solutions":["Inspect response.headers.getSetCookie() to find the malformed cookie string","Sanitize or drop invalid Set-Cookie headers before passing the response to the crawler pipeline","Use a proxy/interceptor to rewrite broken cookie headers at the source","Catch CookieParseError and fall back to a lenient manual cookie parse"],"exampleFix":"// before\nconst cookies = headers.getSetCookie(); // one entry is malformed\n// after\nconst cookies = headers.getSetCookie().filter((c) => /^[^=]+=[^;]*;/.test(c));","handlingStrategy":"validation","validationCode":"const cookieHeaders = response.headers.getSetCookie();\nconst valid = cookieHeaders.filter((c) => /^[^=,;\\s]+=[^;]*;?/.test(c));","typeGuard":null,"tryCatchPattern":"try { return await cookies(response); } catch (e) { if ((e as Error).name === 'CookieParseError') return fallbackManualParse((e as CookieParseError).cookieHeaders); throw e; }","preventionTips":["Sanitize Set-Cookie headers at the proxy layer","Test target sites' headers before full crawls","Filter obviously malformed cookie strings before processing"],"tags":["cookies","http","parsing"],"backgroundTag":"cookie-parse-failed","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}