apify/crawlee · error · CookieParseError

Could not parse cookie header string

Error message

Could not parse cookie header string: ${cookieHeaderString}

What it means

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.

Solutions

  1. Inspect response.headers.getSetCookie() to find the malformed cookie string
  2. Sanitize or drop invalid Set-Cookie headers before passing the response to the crawler pipeline
  3. Use a proxy/interceptor to rewrite broken cookie headers at the source
  4. Catch CookieParseError and fall back to a lenient manual cookie parse

Example fix

// before
const cookies = headers.getSetCookie(); // one entry is malformed
// after
const cookies = headers.getSetCookie().filter((c) => /^[^=]+=[^;]*;/.test(c));
Defensive patterns

Strategy: validation

Validate before calling

const cookieHeaders = response.headers.getSetCookie();
const valid = cookieHeaders.filter((c) => /^[^=,;\s]+=[^;]*;?/.test(c));

Try / catch

try { return await cookies(response); } catch (e) { if ((e as Error).name === 'CookieParseError') return fallbackManualParse((e as CookieParseError).cookieHeaders); throw e; }

Prevention

When it happens

Trigger: Calling processHttpResponse or cookies() on a Response whose headers.getSetCookie() returns one or more malformed cookie strings that Cookie.parse cannot parse.

Common situations: 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.

Related errors


AI-assisted analysis of apify/crawlee@dbe57fb09c (2026-08-30). Data as JSON: /api/errors/00fca6557742beaa. Report an issue: GitHub.

Appendix: source

Thrown at packages/core/src/cookie_utils.ts:22

import { serviceLocator } from './service_locator.js';
import { CookieParseError } from './session_pool/errors.js';

export interface ResponseLike {
    url?: string | (() => string);
    headers?: Record<string, string | string[] | undefined> | (() => Record<string, string | string[] | undefined>);
}

/**
 * @internal
 */
export function getCookiesFromResponse(response: Response): Cookie[] {
    const headers = response.headers;
    const cookieHeaders = headers.getSetCookie();

    try {
        return cookieHeaders.map((cookie) => Cookie.parse(cookie)!);
    } catch (e) {
        throw new CookieParseError(cookieHeaders);
    }
}

/**
 * Calculate cookie expiration date
 * @param maxAgeSecs
 * @returns Calculated date by session max age seconds.
 * @internal
 */
export function getDefaultCookieExpirationDate(maxAgeSecs: number) {
    return new Date(Date.now() + maxAgeSecs * 1000);
}

/**
 * Transforms tough-cookie to puppeteer cookie.
 * @param toughCookie Cookie from CookieJar
 * @return Cookie compatible with browser pool
 * @internal

View on GitHub (pinned to dbe57fb09c)