1

I have a DRF API that implements views that allow GET and POST calls:

@api_view([
    'GET',
    'POST',
])
@cache_page(timeout=60 * 10)
def my_view(request):
  # do stuff

I noticed that:

  1. GET requests get cached as expected.

  2. POST requests do NOT get cached at all.

Questions:

  1. Is this intended or am I doing something wrong?

  2. If this is working as intended...how can I get Django to cache POST requests as well?

ritratt
  • 1,703
  • 4
  • 25
  • 45

1 Answers1

4

From the documentation of the class in Django’s source code:

More details about how the caching works: * Only GET or HEAD-requests with status code 200 are cached. Link: https://github.com/django/django/blob/master/django/middleware/cache.py

The reason for this limitation is probably because GET requests are expected to be idempotent and POST requests are not, therefore it normally doesn’t make sense to cache POST requests. Here is a question that explains this concept nicely: What is idempotency in HTTP methods?

Django is open-source so you can always extend the cache mechanism to get this behavior, but it will probably confuse your API users as they will expect the POST to always be a new request.

user2876375
  • 331
  • 2
  • 4