While working with cURL to send HTTP requests, it's common to interact with web services or sites that use cookies for authentication, session management, or other purposes. Saving these cookies can be beneficial for multiple requests, enabling the reuse of session data or ensuring a persistent login.
By default, cURL doesn't store or read cookies. This is contrary to the behavior of a web browser, which typically manages cookies automatically, making it harder to mimic browser interactions directly with cURL.
However, cURL allows you to save cookies to a file and to utilize them in future requests. Using these saved cookies with cURL enables you to effectively script interactions with web services and maintain session consistency across multiple requests. You can also manually edit the saved cookies file to suit your needs.
$ curl -c cookies.txt https://www.simplified.guide
-c, --cookie-jar <filename> (HTTP) Specify to which file you want curl to write all cookies after a completed operation. Curl writes all cookies from its in-memory cookie storage to the given file at the end of operations. If no cookies are known, no data will be written. The file will be written using the Netscape cookie file format. If you set the file name to a single dash, "-", the cookies will be written to stdout
$ cat cookies.txt # Netscape HTTP Cookie File # https://curl.se/docs/http-cookies.html # This file was generated by libcurl! Edit at your own risk. #HttpOnly_www.simplified.guide FALSE / TRUE 0 DokuWiki a1uvre6dbcc21ujgottgaa5bf0
The file should contain details of all cookies set by the server during your cURL request.
$ curl -b cookies.txt https://www.simplified.guide/logindashboard
With the -b or –cookie option, cURL sends the cookies saved in cookies.txt just like a browser would, ensuring a consistent session or preserving login state.
$ curl -b cookies.txt -c cookies.txt https://www.simplified.guide/loginupdate-profile
Using both -b and -c allows you to send existing cookies and save new ones, updating the cookies.txt file in the process.
$ rm cookies.txt
Always consider security when working with authentication or session cookies. Misuse or exposure can lead to unauthorized access.
Comment anonymously. Login not required.