10

I would like to create a cacheable HTTP response for a POST request.

My actual implementation responses the following for the POST request:

HTTP/1.1 201 Created
Expires: Sat, 03 Oct 2020 15:33:00 GMT
Cache-Control: private,max-age=315360000,no-transform
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 9
ETag: 2120507660800737950
Last-Modified: Wed, 06 Oct 2010 15:33:00 GMT

.........

But it looks like that the browsers (Safari, Firefox tested) are not cacheing the response.

In the HTTP RFC the corresponding part says:

Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields. However, the 303 (See Other) response can be used to direct the user agent to retrieve a cacheable resource.

So I think it should be cached. I know I could set a session variable and set a cookie and do a 303 redirect, but I want to cache the response of the POST request.

Is there any way to do this?

P.S.: I've started with a simple 200 OK, so it does not work.

Thanks,

KARASZI István
  • 28,974
  • 8
  • 95
  • 116
  • The response to a post is usually unique depending on the posted parameters, so it generally makes no sense to cache it. What is your response? I have a feeling you're doing something else not entirely right. – Bart van Heukelom Oct 08 '10 at 10:34
  • It would be a store like thing. If somebody posts something to an URL it will store the content on the specified location for this client. So the client could cache it and does not need to recheck the location because it'll be the same as it posted for this current client. – KARASZI István Oct 08 '10 at 11:29
  • I solved the actual problem with an AJAX GET request with a custom header (for the value). But I'm still interested in the question. – KARASZI István Nov 02 '10 at 13:30
  • Possible duplicate of [Is it possible to cache POST methods in HTTP?](https://stackoverflow.com/questions/626057/is-it-possible-to-cache-post-methods-in-http) – Alexei Levenkov Sep 22 '17 at 18:24

2 Answers2

2

I'd also note that caching is always optional (it's a MAY in the HTTP/1.1 RFC). Since under most circumstances, a successful POST invalidates a cache entry, it's probably simply the case that the browser caches you're looking at just don't implement caching POST responses (since this would be pretty uncommon--usually this is accomplished by formatting things as a GET, which it sounds like you've done).

Jon Moore
  • 1,270
  • 10
  • 11
-1

Can you try to change the Cache-Control to public instead of private and see if it's working?

Nir Levy
  • 4,174
  • 2
  • 29
  • 47