server cookies (not set by me) not saved within a Session

Using a Session, when a server is setting some cookie on a 301/302 response, the cookie is not saved

Update: could not find a public site to show the issue, hope this will be enough!

Update 2: see here http://blog.dubbelboer.com/2012/11/25/302-cookie.html

``` python
http_session = requests.Session()
http_session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64;$
url = 'http://dubbelboer.com/302cookie.php'
r = http_session.get(url)
print(r.history)
print(r.url)
print(r.text)
for cookie in r.cookies:
  print(cookie)
# ---
(Response [302],)
http://dubbelboer.com/302cookie.php?show=1388887790.4324
Cookie was: NOT SET<br><a href="?a=1388887790.4493">try again</a>
<Cookie test= for dubbelboer.com/>
```


@dvasseur I updated your issue to use code blocks so that it's highlighted properly.

Can you let us know which version of requests you're using?


FWIW, On the latest requests this is not an issue for me:

``` pycon
>>> import requests
>>> s = requests.Session()
>>> r = s.get('http://dubbelboer.com/302cookie.php')
>>> r.history
(<Response [302]>,)
>>> s.cookies
<<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='test', value='', port=None, port_specified=False, domain=u'dubbelboer.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=False, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>
>>> r.text
u'Cookie was: SET<br><a href="?a=1388890217.6877">try again</a>'
```


Agreed, this problem does not manifest on my machine on 2.1.0. Try updating to 2.1.0 and try again.


@sigmavirus24 thanks for editing, didn't know how to do it !

I have tested with 2.1.0 (arch version) and installed it again just to be sure

On my machine:

``` python
Python 3.3.3 (default, Nov 26 2013, 13:33:18)
[GCC 4.8.2] on linux
>>> import requests
>>> s = requests.Session()
>>> r = s.get('http://dubbelboer.com/302cookie.php')
>>> r.history
(<Response [302]>,)
>>> s.cookies
<<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='test', value='', port=None, port_specified=False, domain='dubbelboer.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=False, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>
>>> r.text
'Cookie was: NOT SET<br><a href="?a=1388927785.835">try again</a>'
>>> print(requests.__version__)
2.1.0
```


Oooh, weird.

``` python
Python 3.3.3 (default, Nov 20 2013, 19:35:16) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.__version__
'2.1.0'
>>> s = requests.Session()
>>> r = s.get('http://dubbelboer.com/302cookie.php')
>>> r.history
(<Response [302]>,)
>>> r.text
'Cookie was: SET<br><a href="?a=1388928255.3903">try again</a>'
```

This is weird. Time to investigate!


Ubuntu also works fine:

``` python
Python 3.3.2+ (default, Oct  9 2013, 14:50:09) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.__version__
'2.1.0'
>>> s = requests.Session()
>>> r = s.get('http://dubbelboer.com/302cookie.php')
>>> r.history
(<Response [302]>,)
>>> r.text
'Cookie was: SET<br><a href="?a=1388928510.5557">try again</a>'
```


Wait, did you say you installed `requests` from pacman, not `pip`?


yep, does it matter ?


Arch almost certainly apply a lot of downstream patches. Any number of them could break this function. Try using a version obtained from `pip`.


according to this https://projects.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/python-requests the only patch applied is for certificates.

I'll check with the git version here (just need to find out how to import for current dir instead of system repo, i'm a python beginner ;-))


=P If that doesn't work, I'm prepared to believe that there's a patch in the Python standard library around the cookie handling that only Arch applies.


OK, I've checked sys.path, tried with the latest version here and still the same issue. I've also checked default python package in Arch (https://projects.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/python) and there are no patch applied, what next ?


Hmmm. Someone is going to need to confirm that this is related to Arch, and not your specific system. This means either I install arch, or you use a different OS. Not sure how quickly I can get Arch installed today though.


Hmm, I'm have a Suse here also and

``` python
Python 3.3.2 (default, Jun 13 2013, 16:05:31) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.__version__
'2.1.0'
>>> s = requests.Session()
local version
>>> r = s.get('http://dubbelboer.com/302cookie.php')
>>> r.history
(<Response [302]>,)
>>> r.text
'Cookie was: SET<br><a href="?a=1388931312.1533">try again</a>'
```

Working! So it seems to be Arch related :(


Wow, I wonder what weird thing Arch is doing. Want to use `tcpdump` to confirm that the cookie actually isn't being sent to the server on Arch?


I've played with tcpdump and:

```
Set-Cookie: test=1388932595.4576; expires=Sun, 05-Jan-2014 14:36:39 GMT
Location: ?show=1388932595.4576
```

so the cookie is indeed set

BUT look at the `expires` value, it's set to expire right now ! I've checked with a `TZ=GMT date` at the end of my python script and the result is `Sun Jan  5 14:36:40 GMT 2014`, one second later. So in this specific case the cookie doesn't have to exist anymore and requests is doing the right thing!

Could you check this time point on your system ?


So, here's the flow I see from my machine, captured in Wireshark:

```
GET /302cookie.php HTTP/1.1
---> HTTP/1.1 302 Moved Temporarily (received @ Sun, 05-Jan-2014 14:55:52.775986)
     Set-Cookie: test=1388933752.1567; expires=Sun, 05-Jan-2014 14:55:56 GMT
GET /302cookie.php?show=1388933752.1567 HTTP/1.1 (sent @ Sun, 05-Jan-2014 14:55:52.778463)
---> HTTP/1.1 200 OK
```

So, the cookie is set to expire 4 seconds after I received the HTTP response, and Requests sends the redirect fast enough that the cookie doesn't expire. I suspect this is a timing problem on your end.


OK, fine! I've updated my server clock (and set up ntpd BTW) and everything is fine, thanks!