question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set-Cookie header sets cookie in response but not session

See original GitHub issue

When requesting a resource that responds with a Set-Cookie header, the response.cookies returns a RequestsCookierJar with the cookies set in the header, however, the session.cookies returns an empty RequestsCookieJar.

Expected Result

When requesting a resource that responds with the following headers: {'Content-Type': 'text/plain', 'set-cookie': 'foo=bar;'}, the response.cookies should include the foo=bar cookie and the session.cookies should include the foor=bar cookie.

E.g. both should be <RequestsCookieJar[<Cookie foo=bar for />]>

Actual Result

When requesting a resource that responds with the following headers: {'Content-Type': 'text/plain', 'set-cookie': 'foo=bar;'}, the response.cookies does include the foo=bar cookie but the session.cookies remains empty.

response.cookies yields <RequestsCookieJar[<Cookie foo=bar for />]> session.cookies yields <RequestsCookieJar[]>

Reproduction Steps

import requests

session = requests.Session()
response = session.get('http://www.blah.com/')
print(response.cookies)
print(session.cookies)

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.7"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.6.5"
  },
  "platform": {
    "release": "17.3.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.19.1"
  },
  "system_ssl": {
    "version": "100020ff"
  },
  "urllib3": {
    "version": "1.23"
  },
  "using_pyopenssl": false
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nateprewittcommented, Jun 15, 2018

@Audace it’s the same process but performed in two separate places. Here for Session and here for Response.

It would appear something on your mocked response isn’t populating what we’re expecting in r.raw. As I said above, since this doesn’t appear to be a defect in Requests but rather an issue in the mocking setup, we should probably move this to StackOverflow. That will avoid constantly pinging 1300 people watching the issue tracker for bugs in Requests. Feel free to ping the link here, or use the python-requests tag if you create a SO question. Thanks!

0reactions
Audacecommented, Jun 15, 2018

@nateprewitt got it. No this isn’t performing network IO - it’s a mocked response. I added a Content-Length to no avail. That being said, why would there be a difference between response.cookies and session.cookies? Is there a fundamental difference in the cookie extractor for these two jars?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set-Cookie - HTTP - MDN Web Docs
The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent...
Read more >
set-cookie header not working - Stack Overflow
I'm running the server on localhost, with 127.0.0.1 aliased to subdomain-dev.domain.com on port 5080 . My When I receive the response for my ......
Read more >
RFC 6265: HTTP State Management Mechanism
An origin server can include multiple Set-Cookie header fields in a single response. The presence of a Cookie or a Set-Cookie header field...
Read more >
Cookies, document.cookie - The Modern JavaScript Tutorial
Upon sign in, the server uses the Set-Cookie HTTP-header in the response to set a cookie with a unique “session identifier”.
Read more >
HttpOnly - OWASP Foundation
If a browser does not support HttpOnly and a website attempts to set an ... is to overwrite the SET-COOKIE HTTP response header...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found