How to use cURL to send Cookies?

前端 未结 5 1809
孤独总比滥情好
孤独总比滥情好 2020-11-30 17:09

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

I have a REST endpoint as:

class LoginResource(restful.Resource):
    def get(         


        
相关标签:
5条回答
  • 2020-11-30 17:17

    I'm using Debian, and I was unable to use tilde for the path. Originally I was using

    curl -c "~/cookie" http://localhost:5000/login -d username=myname password=mypassword
    

    I had to change this to:

    curl -c "/tmp/cookie" http://localhost:5000/login -d username=myname password=mypassword
    

    -c creates the cookie, -b uses the cookie

    so then I'd use for instance:

    curl -b "/tmp/cookie" http://localhost:5000/getData
    
    0 讨论(0)
  • 2020-11-30 17:18

    You are using a wrong format in your cookie file. As curl documentation states, it uses an old Netscape cookie file format, which is different from the format used by web browsers. If you need to create a curl cookie file manually, this post should help you. In your example the file should contain following line

    127.0.0.1   FALSE   /   FALSE   0   USER_TOKEN  in
    

    having 7 TAB-separated fields meaning domain, tailmatch, path, secure, expires, name, value.

    0 讨论(0)
  • 2020-11-30 17:26

    If you have made that request in your application already, and see it logged in Google Dev Tools, you can use the copy cURL command from the context menu when right-clicking on the request in the network tab. Copy -> Copy as cURL. It will contain all headers, cookies, etc..

    0 讨论(0)
  • 2020-11-30 17:27

    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.

    0 讨论(0)
  • 2020-11-30 17:33

    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
    
    0 讨论(0)
提交回复
热议问题