¿Cómo usar cURL para enviar Cookies?


Leí que Enviar galletas con rizo funciona, pero no para mí

Tengo un REST punto final como

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

Cuando intento acceder como

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"%

Donde mi ~/Downloads/cookies.txt es

cat ~/Downloads/cookies.txt
USER_TOKEN=in

Y el servidor no recibe nada

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 -

¿Qué es lo que me falta?

Author: ivanleoncz, 2013-04-14

3 answers

Esto funcionó para mí

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

Pude ver el valor en el backend usando

print request.cookies
 345
Author: daydreamer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-14 05:32:08

Puede referirse a https://curl.haxx.se/docs/http-cookies.html para un tutorial completo de cómo trabajar con cookies. puede usar

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

Para escribir en un archivo de cookies y arrancar el motor y para utilizar la cookie puede utilizar

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

Para leer las cookies e iniciar el motor de cookies, o si no es un archivo, pasará la cadena dada.

 54
Author: Moeen M,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-22 23:36:18

Está utilizando un formato incorrecto en su archivo de cookies. Como indica curl documentation, utiliza un antiguo formato de archivo de cookies de Netscape, que es diferente del formato utilizado por los navegadores web. Si necesita crear un archivo de cookie curl manualmente, este post debería ayudarlo. En su ejemplo, el archivo debe contener la siguiente línea

127.0.0.1   FALSE   /   FALSE   0   USER_TOKEN  in

7 FICHA campos separados significado dominio, tailmatch, camino, seguro, vence, nombre, valor.

 16
Author: yurloc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-16 16:25:10