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

Caching of POST request, but dependent on data/json #211

Closed
blueyed opened this issue Dec 6, 2019 · 2 comments
Closed

Caching of POST request, but dependent on data/json #211

blueyed opened this issue Dec 6, 2019 · 2 comments

Comments

@blueyed
Copy link

blueyed commented Dec 6, 2019

I'd like to cache responses for POST requests, but only for matching data / json (similar to params have to match for GET requests).

This however does not appear to be possible easily, is it?

Related code: https://github.com/ionrock/cachecontrol/blob/7a198b41a14ac92cf3766599009326165dcc99cd/cachecontrol/controller.py#L125-L148

  • you cannot "encode" it in the request_url, since that only gets request.url (and would be too long anyway etc)
  • the cache lookup also gets the "cache_url" only (not the whole request)
  • the serializer then could return False, but it would mean that there's a misleading log warning, and that only the latest (matching) response would be cached
@blueyed
Copy link
Author

blueyed commented Dec 27, 2019

Note that requests-cache uses the request.body with the cache key: https://github.com/reclosedev/requests-cache/blob/master/requests_cache/backends/base.py#L218-L233

@woodruffw
Copy link
Member

Sorry for the extremely belated response here.

This is indeed not possible, for reasons mentioned in both #216 and #337. The TL;DR is that CacheControl is not architecturally set up to cache POSTs, but also that the HTTP RFCs explicitly preclude caching of state-modifying methods like POST.

If you're working in a particular application context where POST caching makes sense, my recommendation is to either do it via your own custom middleware, or handle it at a higher layer (i.e. write an adapter above your session management to divert POSTs that have been cached).

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

No branches or pull requests

2 participants