Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cURL to send Cookies?

I read that Send cookies with curl works, but not for me.

I have a REST endpoint as:

class LoginResource(restful.Resource):     def get(self):         print(session)         if 'USER_TOKEN' in session:             return 'OK'         return 'not authorized', 401 

When I try to access as:

curl -v -b ~/Downloads/cookies.txt -c ~/Downloads/cookies.txt http://127.0.0.1:5000/ * About to connect() to 127.0.0.1 port 5000 (#0) *   Trying 127.0.0.1... * connected * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.27.0 > Host: 127.0.0.1:5000 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 401 UNAUTHORIZED < Content-Type: application/json < Content-Length: 16 < Server: Werkzeug/0.8.3 Python/2.7.2 < Date: Sun, 14 Apr 2013 04:45:45 GMT < * Closing connection #0 "not authorized"% 

Where my ~/Downloads/cookies.txt is:

cat ~/Downloads/cookies.txt USER_TOKEN=in 

and the server receives nothing:

127.0.0.1 - - [13/Apr/2013 21:43:52] "GET / HTTP/1.1" 401 - 127.0.0.1 - - [13/Apr/2013 21:45:30] "GET / HTTP/1.1" 401 - <SecureCookieSession {}> <SecureCookieSession {}> 127.0.0.1 - - [13/Apr/2013 21:45:45] "GET / HTTP/1.1" 401 - 

What is that I am missing?

like image 741
daydreamer Avatar asked Apr 14 '13 04:04

daydreamer


2 Answers

This worked for me:

curl -v --cookie "USER_TOKEN=Yes" http://127.0.0.1:5000/ 

I could see the value in backend using

print(request.cookies) 
like image 77
daydreamer Avatar answered Oct 11 '22 18:10

daydreamer


You can refer to https://curl.haxx.se/docs/http-cookies.html for a complete tutorial of how to work with cookies. You can use

curl -c /path/to/cookiefile http://yourhost/ 

to write to a cookie file and start engine and to use cookie you can use

curl -b /path/to/cookiefile  http://yourhost/ 

to read cookies from and start the cookie engine, or if it isn't a file it will pass on the given string.

like image 30
Moeen M Avatar answered Oct 11 '22 18:10

Moeen M