使用 If-Modified-Since header 将资源 collection 过滤为 REST API 中的最新资源是否被认为是一种有效的方法?

Is using the If-Modified-Since header to filter a resource collection to only recent ones in a REST API considered a valid approach?

我正在设计一个 REST API,我需要提供选项以仅获取 collection 中最近创建或修改的资源,基于 [=30] =] 时间戳(反过来,这将在之前的响应中由 API 生成)。为此,我正在考虑使用 Last-Modified 和 If-Modified-Since headers。

较早的问题在这里(如 Is it valid to modify a REST API representation based on a If-Modified-Since header?) seems to indicate that this is frowned upon, on the grounds that RFC2616 indicates that the purpose of these headers is related to caching. However, since then, RFC2616 has been superseded by RFC7232,其中指出

If-Modified-Since is typically used for two distinct purposes: 1) to allow efficient updates of a cached representation that does not have an entity-tag and 2) to limit the scope of a web traversal to resources that have recently changed.

我的解释是,我的用例允许检索自上次检索以来对 collection 的所有更改已包含在第二个目的中。

所以我有两个问题:

  1. 这个解释是否正确,或者我在这里遗漏了一些微妙的东西?
  2. 即使我的解释是正确的,以这种方式使用这些 headers 是否是一种好的做法?换句话说:还有什么其他原因不使用这些 headers 而不是,例如,在响应中包含一个时间戳,并允许客户端在下一个请求的查询字符串中提供该时间戳?

Is this interpretation correct, or am I missing something subtle here?

我认为RFC 7234与您的解释相矛盾。

If an If-None-Match header field is not present, a request containing an If-Modified-Since header field (Section 3.3 of [RFC7232]) indicates that the client wants to validate one or more of its own stored responses by modification date. A cache recipient SHOULD generate a 304 (Not Modified) response (using the metadata of the selected stored response) if one of the following cases is true....

这里的主要问题是通用缓存不会知道您的资源/您的服务器对标准 headers 的含义有不同的理解,因此客户不会知道你想要的体验。

此外...

I'm designing a REST API where I have a need to provide the option to GET only the resources in a collection that were created or modified recently, based on a client-provided timestamp (which, in turn, will have been generated by the API in a previous response).

我们已经为此制定了标准化机制 - 即 URI。如果您查看菲尔丁对 resource.

的定义,这可能会变得更清楚

我是这样理解的:在 REST 的上下文中,“资源”是“文档”的泛化(另请参见 Jim Webber, 2011)。从相同(或重叠)信息派生出许多不同文档是完全合理的。

想想“分页”——每一页都是一个不同的文档,有自己唯一的标识符,但所有页面都来自同一个公共源,随着时间的推移,项目从一页移动到另一页。