Cookies not propagated to requests.Session

Bug #1550980 reported by Alex
24
This bug affects 4 people
Affects Status Importance Assigned to Milestone
requests-mock
Confirmed
Medium
Unassigned

Bug Description

I'm having problems setting cookies using the requests_mock header= option. A cookie makes it as far as the requests.Response object, but does not reach the requests.Session I used to make it. I've tried several variants of the Set-Cookie header and believe I have it formatted correctly.

The following code demonstrates it:

import requests, requests_mock

with requests_mock.Mocker() as m:
  m.register_uri('GET', 'http://github.com', text='data', headers={
    "Set-Cookie": "food=pie; Max-Age=3600; Version=1"
  })
  sess = requests.Session()
  resp = sess.get("http://github.com")
  for c in resp.cookies:
    print("resp cookie: %s" % c)
  for c in sess.cookies:
    print("sess cookie: %s" % c)

Expected result: 'food=pie' cookie listed in both resp and sess cookies. Running the code outside of the "with" does this.

Actual result: 'food=pie' cookie listed in only resp and is not sent back to the server in further requests.

Changed in requests-mock:
status: New → Confirmed
importance: Undecided → Medium
Revision history for this message
Jamie Lennox (jamielennox) wrote :

You're right, unfortunately this is a harder problem to fix than i would expect.

When requests parses the cookies off the response object and onto the session it doesn't read it the cookiejar on the response object, it reads the headers from the httplib response object that urllib3 provides "for debugging purposes". I'm not sure if this is to provide seperate handling for session cookies, it just seems to be old code.

There are a couple of places i fake this interface because all we need are the headers and it generally works, and I can fix your sample case pretty easily because it is in cookie header format. However i'm stuck with how to handle the case where a user provides a cookiejar object. To make this work i would need to convert the cookiejar back into Set-Cookie headers so that requests could parse them again. There's no obvious interface to do this in python - cookiejars aren't supposed to work like that.

The solution then is to just dump the cookie objects from the jar and construct the headers myself. This might be the only way but i'd really prefer not to maintain that/learn that much about cookies if i can avoid it.

Ideally It would be nice if requests fixed there interface here but that doesn't solve the problem for existing versions.

I'll keep looking, maybe Ian can lend some advice here?

Revision history for this message
Pablo Pizza (pizza12) wrote :

The problem seems still present today. Is the bug due to python-requests? If it is, maybe the bug could be forwarded to them?

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.