Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST that sets cookies will not cache #23

Closed
saulshanabrook opened this issue Jun 26, 2014 · 4 comments
Closed

POST that sets cookies will not cache #23

saulshanabrook opened this issue Jun 26, 2014 · 4 comments
Labels

Comments

@saulshanabrook
Copy link

This will not cache, for some reason
requests.post("http://httpbin.org/cookies/set?name=dfd")

@reclosedev
Copy link
Collaborator

First of all httpbin returns 405 status for POST /cookies

In [7]: requests.post("http://httpbin.org/cookies/set?name=dfd").text
Out[7]: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>405 Method Not Allowed</title>\n<h1>Method Not Allowed</h1>\n<p>The method is not allowed for the requested URL.</p>\n'

Also, did you add 'POST' to allowable_methods args?
http://requests-cache.readthedocs.org/en/latest/api.html#requests_cache.core.CachedSession e.g.

In [14]: requests_cache.install_cache(allowable_methods=('GET', 'POST'))
In [15]: requests.post("http://httpbin.org/post", cookies={"foo": "bar"}).from_cache
Out[15]: False

In [16]: requests.post("http://httpbin.org/post", cookies={"foo": "bar"}).from_cache
Out[16]: True

In [17]: requests.post("http://httpbin.org/post", cookies={"foo": "bar"}).json()
Out[17]:
{'args': {},
 'files': {},
 'origin': 'xxxxxxxx',
 'json': None,
 'headers': {'Connection': 'close',
  'Accept': '*/*',
  'Cookie': 'foo=bar',
  'X-Request-Id': '7e4e122f-9b1a-435d-86ba-7ab06d2dc16b',
  'Content-Length': '0',
  'User-Agent': 'python-requests/2.3.0 CPython/3.4.0 Linux/3.13.0-24-generic',
  'Accept-Encoding': 'gzip, deflate',
  'Host': 'httpbin.org'},
 'url': 'http://httpbin.org/post',
 'form': {},
 'data': ''}

@saulshanabrook
Copy link
Author

Yeah I did add POST to allowable methods. Is there a httpbin for sending a POST that sets cookies?

@reclosedev
Copy link
Collaborator

Looks like no. But check my example. It sets cookies while requesting http://httpbin.org/post

@saulshanabrook
Copy link
Author

You are right, I figured out what my problem was. When passing in a dictionary as any argument, the order is not defined. So when you do the same post twice, with the same options, it can be cached differently because the order changes for the dictionary. I got the idea from here: http://cachecontrol.readthedocs.org/en/latest/tips.html#query-string-params

So to fix I turned the dictionary into a list of lists and passed that in sorted:

s.post('url', sorted([key, value] for key, value in options.items()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants