These curl recipes show you how to add cookies to curl requests. By default, curl doesn't send any cookies but you can add your own cookies via the -b 'name=value'
command line argument. To save cookies from the response to a file, use the -c file
option. To load cookies from a file, use the -b file
option.
Add a Cookie
curl -b 'session=abcdef' https://google.com
This recipe uses the -b name=value
argument to set a cookie with the name session
to the value abcdef
in a GET request to https://google.com
. The full header that curl sets for this request looks like this Cookie: session=abcdef
.
Add Two Cookies
curl -b 'session=abcdef' -b 'loggedin=true' https://google.com
This recipe uses the -b name=value
argument twice and sets two cookies. The first cookie is the same as in the previous recipe and the second cookie is loggedin
with the value true
.
Add an Empty Cookie
curl -b 'session=' https://google.com
This recipe uses only the first half of -b name=value
option and only sets the cookie name to session
but leaves the cookie value empty. In this case, curl sends an empty cookie that in the HTTP header looks like this Cookie: session=
.
Save Cookies to a File
curl -c cookies.txt https://www.google.com
This recipe uses the -c cookies.txt
option that saves the response cookies to cookies.txt
file. When curl makes a GET request to https://www.google.com
, the web server responds with one or more Set-Cookie: name=value
header values. Curl takes them and saves them to a file that you can load via -b cookies.txt
(see the next recipe).
Load Cookies from a File
curl -b cookies.txt https://www.google.com
This recipe uses the -b cookies.txt
option that loads cookies from the cookies.txt
file. Notice that curl uses the -b
option to both set cookies on the command line (when the argument is name=value
) and to load cookies from a file (when the argument doesn't contain the =
symbol.)
Created by Browserling
These curl recipes were written down by me and my team at Browserling. We use recipes like this every day to get things done and improve our product. Browserling itself is an online cross-browser testing service powered by alien technology. Check it out!
Secret message: If you love my curl recipe, then I love you, too! Use coupon code CURLLING to get a discount at my company.