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.
Related: How to save cookies from cURL request
Related: How to use cookies in cURL requests
Steps to create a cookie file for cURL:
- Launch your preferred text editor.
- Construct your cookie file using the Netscape cookie file format.
# Domain Tailmatch Path Secure Expiry Name Value
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Save your manually constructed cookie with a txt extension.
- 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.
- Use the created cookie file in your cURL commands.
$ curl -b "manual-cookies.txt" http://www.example.com/dashboard
Related: How to use cookies in cURL requests
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.