如何缓存一个HTTP POST响应? [英] How to cache an HTTP POST response?
问题描述
我想创建一个POST请求的缓存HTTP响应。
I would like to create a cacheable HTTP response for a POST request.
我的实际执行的响应为POST请求如下:
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
.........
但它看起来像(火狐测试Safari浏览器,)浏览器不cacheing响应。
But it looks like that the browsers (Safari, Firefox tested) are not cacheing the response.
在 HTTP RFC 的相应部分说:
该方法的响应不能缓存,除非响应包括适当的Cache-Control或Expires头字段。然而,303(见其他)响应可以用来指示用户代理来检索缓存资源。
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.
所以,我认为它应该被缓存。我知道我可以设置一个会话变量并设置cookie,并做303重定向,但我想缓存POST请求的响应。
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:我已经开始与一个简单的 200 OK
,所以这是行不通的。
P.S.: I've started with a simple 200 OK
, so it does not work.
谢谢,
推荐答案
我也注意到,缓存始终是可选的(这是在HTTP / 1.1 RFC可以)。因为在大多数情况下,一个成功的POST的的失效的缓存条目,这可能仅仅是浏览器缓存你看就是不实现缓存POST响应(因为这将是$ P $的情况下ptty少见 - 这通常是通过格式化的东西作为一个GET,它听起来像你这样做)完成
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).
这篇关于如何缓存一个HTTP POST响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!