How to cache an HTTP POST response?

2019-04-06 04:35发布

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,

2条回答
狗以群分
2楼-- · 2019-04-06 05:10

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).

查看更多
干净又极端
3楼-- · 2019-04-06 05:12

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

查看更多
登录 后发表回答