Cookies store session data, preferences, and authentication states, enabling persistent, stateful HTTP interactions. The Netscape HTTP cookie file format standardizes how cookies are represented outside of a browser.

By creating a cookie file manually for cURL, it’s possible to control which cookies get sent during requests. This approach aids in automation, testing specific conditions, or replicating browser-like sessions without a full browser.

Understanding the Netscape HTTP cookie file format ensures compatibility with cURL and other tools. Precisely formatted entries for domain, path, and expiration enable reproducible and tailored HTTP interactions.

  1. Open a text editor.
  2. Start with a comment line indicating the Netscape HTTP cookie file format.
    # Netscape HTTP Cookie File
    # Domain    Tailmatch  Path  Secure  Expiry      Name       Value
  3. Specify the domain starting with a dot.
    .example.com

    A leading dot applies the cookie to all subdomains.

  4. Set the tailmatch field to TRUE or FALSE.
    .example.com TRUE

    TRUE allows the cookie to span subdomains.

  5. Define the path scope of the cookie.
    .example.com TRUE    /

    “/” applies the cookie to the entire site.

  6. Specify if the cookie is secure.
    .example.com TRUE    /   TRUE

    TRUE means the cookie is only sent over HTTPS.

  7. Add an expiration time as a UNIX timestamp.
    .example.com TRUE    /   TRUE   1672531199

    This timestamp controls when the cookie expires.

  8. Provide the cookie’s name and value.
    .example.com TRUE    /   TRUE   1672531199 SessionID 1234567890abcdef

    Name-value pairs define the cookie’s data.

  9. Add more cookies as needed.
    .example.com TRUE    /   TRUE   1672531199 UserID myid

    Each line represents one cookie entry.

  10. Save the file with a .txt extension.
  11. Use the cookie file in cURL requests.
    $ curl --cookie "manual-cookies.txt" "http://www.example.com/dashboard"
Discuss the article:

Comment anonymously. Login not required.