rest - 为什么对 PUT 请求的响应不能提供 ETag?

标签 rest http put etag rfc

来自 Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content :

An origin server MUST NOT send a validator header field (Section 7.2), such as an ETag or Last-Modified field, in a successful response to PUT unless the request's representation data was saved without any transformation applied to the body (i.e., the resource's new representation data is identical to the representation data received in the PUT request) and the validator field value reflects the new representation. This requirement allows a user agent to know when the representation body it has in memory remains current as a result of the PUT, thus not in need of being retrieved again from the origin server, and that the new validator(s) received in the response can be used for future conditional requests in order to prevent accidental overwrites (Section 5.2).

我不能完全理解这部分... 加粗的句子似乎自相矛盾,不是吗?

请注意,PUT 是唯一包含验证器 header 部分的动词(请参阅 GET/POST/DELETE/PATCH)。

最佳答案

关键是服务器可能会也可能不会在存储之前更改表示。从您链接到的部分:

A successful PUT of a given representation would suggest that a subsequent GET on that same target resource will result in an equivalent representation being sent in a 200 (OK) response. However, there is no guarantee that such a state change will be observable, since the target... might be subject to dynamic processing by the origin server.

因此,该标准使用是否存在验证器 header 来向用户代理指示表示是否已更改。

如果表示没有被改变,那么服务器可以返回验证器头字段,用户代理可以使用它来有条件地验证它刚刚发送的表示。

如果表示已更改,则根据定义,用户代理的表示无效。因此不会返回任何验证器 header ,用户代理将不得不执行无条件的 GET

关于rest - 为什么对 PUT 请求的响应不能提供 ETag?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246577/

相关文章:

Java EE TDD 起点

java - 使用 Rest API 在 Azure IoT 中心创建共享访问策略

java - 使用 jackson databind 将 JSON 反序列化为 Map<String,String>

security - 通过 HTTP 的 session 劫持

C# RestSharp 阻止 302 上的请求重定向

xml - Spring Boot 找不到可接受的 XML 响应表示

java - HTTP PUT 请求的格式是什么?

web-services - 在 RESTful 服务中部分更新复杂类型

json - Clojure Noir Json Put

php - 如何使用PHP通过HTTP PUT接收文件