Session dying when using curl from command line Session dying when using curl from command line curl curl

Session dying when using curl from command line


If you want curl to store cookies between calls, you need to tell it to do so.

Check out the --cookie and --cookie-jar options.

curl --cookie-jar cookies.txt -s -L --data "secret=secret_password"# thencurl --cookie cookies.txt --cookie-jar cookies.txt -s -L url/films

The --cookie-jar option tells curl to write any cookies it received to the file when the request ends, and the --cookie option instructs it to read cookies from that file for issuing requests.

That should allow you to log in, and then use the cookies to re-establish the session on subsequent requests.

curl --help and man curl are your friends!


Complementing the great @drew010 answer, you can also use both --cookie and --cookie-jar together if you need to read and (also) write/update the cookie.

Example of bash script using cURL, reading and writing cookies, and storing them in the /temp folder (to keep your local folder clean):

#!/bin/bashecho "## SUBMITTING..."; sleep 0.5;echo "## SERVER RESPONSE:";echo ""; sleep 0.5;curl -X GET \  http://localhost:8080/web-service/path \  --cookie /tmp/cookie.txt \  --cookie-jar /tmp/cookie.txt \  -H 'Accept: */*' \  -H 'Cache-Control: no-cache' \  -H 'Connection: keep-alive' \  -H 'Content-Type: application/json' \echo "";echo "";