I know that using cURL
I can see my received cookies / headers by using
curl --head www.google.com
And I know that I can add headers to my request using
curl --cookie "Key=Value" www.google.com
I am currently working on testing an issue which requires persistent cookies, and there can be a lot of them.
How can I efficiently preserve cookies between two
cURL
requests?
If possible using a temporary file for storage.
curl -b cookie.txt -c cookie.txt <url>
orcurl --cookie cookie.txt --cookie-jar cookie.txt <url>
will both store and send saved cookies.Use the
--cookie-jar
or--dump-header
parameter to save received cookies to a file. The--cookie
parameter can read back the cookies from that file later.Alternatively, instead of using the command-line cURL app, write some code that uses the libCurl library. That will give you more direct control over cookie handling. libCurl has several features related to HTTP cookies:
Options for
curl_easy_getinfo()
:Options for
curl_easy_setopt()
:CURLOPT_COOKIE - set contents of HTTP Cookie header
CURLOPT_COOKIEFILE - file name to read cookies from
CURLOPT_COOKIEJAR - file name to store cookies to
CURLOPT_COOKIESESSION - start a new cookie session
CURLOPT_COOKIELIST - add to or manipulate cookies held in memory
Then you can store the cookies however you want, and assign them as needed to later HTTP sessions.