Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send cookies with curl

Tags:

curl

cookies

I am using curl to retrieve cookies like so:

curl -c cookies.txt url 

then I parse the cookie I want from the cookies.txt file and send the request again with the cookie

curl -b "name=value" url  

Is this the correct way to send the cookie? Is there a simpler way?

like image 271
dmz73 Avatar asked Aug 24 '11 20:08

dmz73


People also ask

How do you send cookies with curl?

Cookies are passed to Curl with the --cookie "Name=Value" command line parameter. Curl automatically converts the given parameter into the Cookie: Name=Value request header. Cookies can be sent by any HTTP method, including GET, POST, PUT, and DELETE, and with any data, including JSON, web forms, and file uploads.

Does curl handle cookies?

curl has a full cookie "engine" built in. If you just activate it, you can have curl receive and send cookies exactly as mandated in the specs. tell curl a file to read cookies from and start the cookie engine, or if it is not a file it will pass on the given string.

How do you curl multiple cookies?

To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons.

How do I send a curl message?

To POST a file with curl , simply add the @ symbol before the file location. The file can be an archive, image, document, etc.


1 Answers

You can use -b to specify a cookie file to read the cookies from as well.

In many situations using -c and -b to the same file is what you want:

curl -b cookies.txt -c cookies.txt http://example.com 

Further

Using only -c will make curl start with no cookies but still parse and understand cookies and if redirects or multiple URLs are used, it will then use the received cookies within the single invoke before it writes them all to the output file in the end.

The -b option feeds a set of initial cookies into curl so that it knows about them at start, and it activates curl's cookie parser so that it'll parse and use incoming cookies as well.

See Also

The cookies chapter in the Everything curl book.

like image 127
Daniel Stenberg Avatar answered Sep 20 '22 05:09

Daniel Stenberg