简体   繁体   中英

'Missing' cookies in python requests response

I have a working cURL command, which gets a number of cookies from this particular (alfresco, although I don't think this is particularly an alfresco issue), however when I try and encode the same request with python requests, I get only one cookie.

The cURL request is

curl -v --junk-session-cookies -H 'Content-Type: application/x-www-form-urlencoded' --cookie-jar cookies.txt --cookie cookies.txt -H 'Origin: http://<url>' -D headers.txt -e ';auto' -X POST -d @credentials http://<url>/share/page/dologin

And the python is

r=s.post('%s/share/page/dologin' % <url>,
           data=credentials, allow_redirects=False)

where

s.headers
Out[121]: {'Origin': <url>, 'Host' :<url>, 'referer':<url> , 'Accept': '*/*', 'User-Agent': 'python-requests/2.4.1 CPython/2.7.5+ Linux/3.11.0-26-generic','Content-Type': 'application/x-www-form-urlencoded'}

designed to closely match the headers that cURL is sending in. The allow_redirects=False is there to mirror the lack of the -L option.

cURL yields:

Added cookie JSESSIONID="<removed>" for domain 10.12.3.166, path /share/, expire 0 < Set-Cookie: JSESSIONID=<removed>; Path=/share/; HttpOnly * Added cookie alfLogin="<removed>" for domain 10.12.3.166, path /share, expire 1413289198 < Set-Cookie: alfLogin=<removed>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share * Added cookie alfUsername3="<obf>" for domain 10.12.3.166, path /share, expire 1413289198 < Set-Cookie: alfUsername3=<obf>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share

Whereas I only get the JSESSIONID cookie in requests.

Update I foolishly failed to include the headers sent in the cURL command, and those received. Those sent were

> User-Agent: curl/7.32.0
> Host: <url>
> Accept: */*
> Referer: 
> Content-Type: application/x-www-form-urlencoded
> Origin: <url>
> Content-Length: 44

And the headers sent back are

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=<rem>; Path=/share/; HttpOnly
Set-Cookie: alfLogin=<rem>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share
Set-Cookie: alfUsername3=<rem>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share
Location: <url>/share

The headers sent back to requests are

{'transfer-encoding': 'chunked', 'set-cookie': 'JSESSIONID=<rem>; Path=/share/; HttpOnly', 'server': 'Apache-Coyote/1.1', 'connection': 'close', 'date': 'Wed, 08 Oct 2014 08:54:53 GMT', 'content-type': 'text/html;charset=ISO-8859-1'}

The solution was to remove the "Host" header and set the referer field to be ''. I then got all the cookies I needed, so thanks to all the helpful comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM