Cookies are small pieces of data stored on a user's computer by a web browser. They store user session information, such as login credentials, shopping cart contents, or user preferences. Cookies are sent to the server with each request, allowing the server to identify the user and provide a personalized experience.

cURL supports the Netscape cookie file format, which is used by most browsers, including Chrome, Firefox, and Safari. The cookie file is a plain text file with one cookie per line. Each line contains seven tab-separated fields, such as domain, path, and expiration time.

You can create a cookie file manually to use with cURL. You can also save cookies from a browser session and edit them manually to create a custom cookie file for cURL.

  1. Launch your preferred text editor.
  2. Construct your cookie file using the Netscape cookie file format.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value
  3. Set the domain where the cookie is valid.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value 
    .example.com

    The domain should begin with a dot, which indicates that the cookie is valid for subdomains as well. If not, the cookie is valid only for that specific domain.

  4. Declare whether all machines within the given domain can access the cookie.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value 
    .example.com  TRUE

    “TRUE” means the cookie is accessible by any machine within the specified domain. “FALSE” indicates the cookie is restricted only to the specified domain.

  5. Indicate the path within the domain where the cookie is valid.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value 
    .example.com  TRUE      /

    The path specifies the directory or directories that the cookie is valid for. A path of “/” means the cookie is available for the entire website.

  6. Specify if the cookie requires a secure connection.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value 
    .example.com  TRUE      /     TRUE

    If set to TRUE, the cookie will only be sent over secure connections like HTTPS. If set to FALSE, the cookie can be sent over any connection.

  7. Set the cookie expiration time.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value 
    .example.com  TRUE      /     TRUE    1672531199

    This UNIX timestamp determines when the cookie will expire. After this time, the cookie will no longer be sent in requests to the server.

  8. Set the cookie name.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value 
    .example.com  TRUE      /     TRUE    1672531199  SessionID

    This is the identifier for the cookie and is used by the server to retrieve its value.

  9. Set the cookie value.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value 
    .example.com  TRUE      /     TRUE    1672531199  SessionID 1234567890abcdef

    The value holds the actual data stored for the cookie and is sent to the server in each request.

  10. Save your manually constructed cookie with a txt extension.
  11. Add a new line to add another cookie in the same file.
    # Domain      Tailmatch Path  Secure  Expiry      Name      Value
    .example.com  TRUE      /     TRUE    1672531199  SessionID 1234567890abcdef
    .example.com  TRUE      /     TRUE    1672531199  UserID    myid

    Ensure cookies are in the correct order; entries above take precedence.

  12. Use the created cookie file in your cURL commands.
    $ curl -b "manual-cookies.txt" http://www.example.com/dashboard
Discuss the article:

Comment anonymously. Login not required.