Curl 发送信息带Cookies

11 min read
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.

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.)