How to Save Cookies and Maintain Sessions with cURL

Tadashi Shigeoka ·  Thu, February 25, 2021

I’ll share how to save cookies and maintain sessions with curl.

Linux | リナックス

Background: Unable to Maintain Sessions with curl

I was having trouble maintaining sessions because curl was issuing new sessions every time I sent HTTP requests.

Cause: curl Doesn't Save Cookies by Default

It seems curl doesn’t save cookies unless you use the -c option, so sessions couldn’t be maintained.

How to Save Cookies and Maintain Sessions with curl

The solution is to use curl’s -c option to specify a file for saving cookies.

curl -c Cookie file name

For example, if you want to maintain a logged-in session by saving cookies, use -c
curl -c cookie.txt http://example.com/login

curl -b Cookie file name to use

curl -b cookie.txt http://example.com/mypage

That’s all from wanting to save cookies and maintain sessions with curl from the Gemba.