{"action":"created","issue":{"url":"https://api.github.com/repos/kennethreitz/requests/issues/2532","labels_url":"https://api.github.com/repos/kennethreitz/requests/issues/2532/labels{/name}","comments_url":"https://api.github.com/repos/kennethreitz/requests/issues/2532/comments","events_url":"https://api.github.com/repos/kennethreitz/requests/issues/2532/events","html_url":"https://github.com/kennethreitz/requests/issues/2532","id":66505311,"number":2532,"title":"\"PreparedRequest.prepare_cookies\" can only be called once (and \"succeeds\" silently if called again)","user":{"login":"smiley","id":354181,"avatar_url":"https://avatars.githubusercontent.com/u/354181?v=3","gravatar_id":"","url":"https://api.github.com/users/smiley","html_url":"https://github.com/smiley","followers_url":"https://api.github.com/users/smiley/followers","following_url":"https://api.github.com/users/smiley/following{/other_user}","gists_url":"https://api.github.com/users/smiley/gists{/gist_id}","starred_url":"https://api.github.com/users/smiley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smiley/subscriptions","organizations_url":"https://api.github.com/users/smiley/orgs","repos_url":"https://api.github.com/users/smiley/repos","events_url":"https://api.github.com/users/smiley/events{/privacy}","received_events_url":"https://api.github.com/users/smiley/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/kennethreitz/requests/labels/Contributor%20Friendly","name":"Contributor Friendly","color":"0b02e1"},{"url":"https://api.github.com/repos/kennethreitz/requests/labels/Documentation","name":"Documentation","color":"fad8c7"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-04-06T01:49:17Z","updated_at":"2015-04-06T23:19:25Z","closed_at":"2015-04-06T23:19:25Z","body":"I'm writing an adapter that interfaces with a CDN, and it requires that I \"sign\" my requests with a cookie, but it doesn't send the cookie back in `Set-Cookie`.\r\n\r\nI manage to send it on the first request, but then the site behind the CDN *does* send a `Set-Cookie` header (this is expected), and `prepare_cookies` called by the `Session` works its magic. The problem is, due to how `cookielib` is written, if a `Cookie` header was already composed, it won't be re-composed: (Even if cookies have changed between calls)\r\n\r\n```python\r\n# cookielib.py:CookieJar.add_cookie_header (starts at 1312)\r\n# ...\r\nattrs = self._cookie_attrs(cookies)\r\nif attrs:\r\n    if not request.has_header(\"Cookie\"):\r\n        request.add_unredirected_header(\r\n          \"Cookie\", \"; \".join(attrs))\r\n#...\r\n```\r\n\r\nThis causes a problem when any request past the first one on the same `Session` object renders my adapter worthless, as authentication fails and attempts to properly add cookies fail silently:\r\n```python\r\ndef _prepare_request(self, request):\r\n    \"\"\"\r\n    :type request: requests.PreparedRequest\r\n    \"\"\"\r\n    request.prepare_cookies({self.COOKIE_NAME: self._get_authorization_cookie(request.url)})\r\n```\r\n\r\n`request._cookies` is modified properly, but `request.headers['Cookie']` is still its former self.\r\n\r\nIf fixing this isn't possible (it *is* a bundled library after all), maybe `requests` could delete the header before calling `CookieJar.add_cookie_header`?"},"comment":{"url":"https://api.github.com/repos/kennethreitz/requests/issues/comments/90281210","html_url":"https://github.com/kennethreitz/requests/issues/2532#issuecomment-90281210","issue_url":"https://api.github.com/repos/kennethreitz/requests/issues/2532","id":90281210,"user":{"login":"sigmavirus24","id":240830,"avatar_url":"https://avatars.githubusercontent.com/u/240830?v=3","gravatar_id":"","url":"https://api.github.com/users/sigmavirus24","html_url":"https://github.com/sigmavirus24","followers_url":"https://api.github.com/users/sigmavirus24/followers","following_url":"https://api.github.com/users/sigmavirus24/following{/other_user}","gists_url":"https://api.github.com/users/sigmavirus24/gists{/gist_id}","starred_url":"https://api.github.com/users/sigmavirus24/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sigmavirus24/subscriptions","organizations_url":"https://api.github.com/users/sigmavirus24/orgs","repos_url":"https://api.github.com/users/sigmavirus24/repos","events_url":"https://api.github.com/users/sigmavirus24/events{/privacy}","received_events_url":"https://api.github.com/users/sigmavirus24/received_events","type":"User","site_admin":false},"created_at":"2015-04-06T23:19:25Z","updated_at":"2015-04-06T23:19:25Z","body":"Yeah, I wasn't really going to bring up the fact that adapters aren't the right place for this logic. That said, I didn't take very much time to try to understand what you were doing with the code, so giving you a way to refactor it isn't quite possible for me at the moment. I would suspect that subclassing a session would be much cleaner and simpler for you, especially since I noticed that you're calling `requests.get` at one point inside an adapter method.\r\n\r\nThat aside, I'd be happy to discuss this on an issue on cloudflare-scrape or via email. This issue, I believe, can be closed."}}