{"record":{"id":"bd39fb9396571d58","repo":"D4Vinci/Scrapling","slug":"could-not-parse-cookie-string-from-header-header","errorCode":null,"errorMessage":"Could not parse cookie string from header '{header_value}': {e}","messagePattern":"Could not parse cookie string from header '(.+?)': (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"scrapling/core/utils/_shell.py","lineNumber":42,"sourceCode":"    for header_line in header_lines:\n        if \":\" not in header_line:\n            if header_line.endswith(\";\"):\n                header_key = header_line[:-1].strip()\n                header_value = \"\"\n                header_dict[header_key] = header_value\n            else:\n                raise ValueError(f\"Could not parse header without colon: '{header_line}'.\")\n        else:\n            header_key, header_value = header_line.split(\":\", 1)\n            header_key = header_key.strip()\n            header_value = header_value.strip()\n\n            if parse_cookies:\n                if header_key.lower() == \"cookie\":\n                    try:\n                        cookie_dict = {key: value for key, value in _CookieParser(header_value)}\n                    except Exception as e:  # pragma: no cover\n                        raise ValueError(f\"Could not parse cookie string from header '{header_value}': {e}\")\n                else:\n                    header_dict[header_key] = header_value\n            else:\n                header_dict[header_key] = header_value\n\n    return header_dict, cookie_dict\n","sourceCodeStart":24,"sourceCodeEnd":49,"githubUrl":"https://github.com/D4Vinci/Scrapling/blob/5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f/scrapling/core/utils/_shell.py#L24-L49","documentation":"Raised by `_ParseHeaders` in scrapling/core/utils/_shell.py:42 when a `Cookie:` header line is found and cookie parsing is enabled (the default). The header's value is fed through `_CookieParser` (an `http.cookies.SimpleCookie` derivative); if that parser throws — typically malformed cookie syntax that even the lenient parser rejects — the original exception is wrapped in a ValueError showing the cookie string. Marked `# pragma: no cover`, so it is a defensive path for badly malformed cookie strings.","triggerScenarios":"Passing `-H 'Cookie: session=abc; ; ;'` or a cookie value with illegal characters/unbalanced quotes that `SimpleCookie` cannot parse, while `parse_cookies=True` (default for `scrapling shell`/CLI fetch). `_ParseHeaders(lines, parse_cookies=False)` (used by `extra_headers` paths in cli.py) never raises this.","commonSituations":"Pasting a Cookie header copied from DevTools where encoding got mangled (e.g. quotes stripped by the shell); cookie values containing `\"` or control characters; hand-typing a cookie string with stray separators.","solutions":["Fix the cookie string to standard `name=value; name2=value2` syntax and single-quote the whole `-H` argument in the shell.","URL-encode exotic characters in cookie values or drop cookies that aren't needed.","If you don't need cookie parsing, pass headers through a path with `parse_cookies=False` (e.g. `--extra-headers` handling in the CLI) or use the `-b/--cookie` style option per cookie.","Test the string in Python first: `dict(http.cookies.SimpleCookie())` behavior with `_CookieParser` to see the underlying error."],"exampleFix":"# before\nscrapling shell -H 'Cookie: session=abc\"; ; bad==' https://example.com\n\n# after\nscrapling shell -H 'Cookie: session=abc; theme=dark' https://example.com","handlingStrategy":"validation","validationCode":"from http.cookies import SimpleCookie\n\ndef cookie_string_ok(cookie_value: str) -> bool:\n    try:\n        c = SimpleCookie()\n        c.load(cookie_value)\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    headers, cookies = _ParseHeaders(lines)\nexcept ValueError as e:\n    if 'cookie' in str(e).lower():\n        headers, _ = _ParseHeaders([l for l in lines if not l.lower().startswith('cookie')])\n        # re-add a cleaned cookie header manually\n    else:\n        raise","preventionTips":["Keep cookie strings in 'name=value; name2=value2' form.","Single-quote the entire Cookie header in the shell to protect special characters.","Test malformed inputs with SimpleCookie before feeding them to the CLI."],"tags":["cli","cookies","headers","parsing","scrapling"],"backgroundTag":null,"analyzedSha":"5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f","analyzedAt":"2026-08-14T22:23:09.440Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}