[英]Should REST api PUT accept a request with a single edited property and the id of the document?
Or should the client send every time every property even if they were not edited?或者客户应该每次发送每个属性,即使它们没有被编辑?
So imagine on the server I have stored in my DB the following document:所以想象一下我在我的数据库中存储的服务器上的以下文档:
{
_id: "1",
name: "Mario",
surname: "Rossi"
}
Now imagine the client wants to update JUST the name.现在假设客户端只想更新名称。 Should this PUT request be acceptable?这个 PUT 请求应该可以接受吗?
{
_id: "1",
name: "Giorgio"
}
Or should the client send all properties?还是客户端应该发送所有属性?
{
_id: "1",
name: "Giorgio",
surname: "Rossi"
}
It seems like partial edits can save some bandwidth and it does not really make sense to send extra data, but I'm not sure about what's the standard here.似乎部分编辑可以节省一些带宽,发送额外数据实际上没有意义,但我不确定这里的标准是什么。 Thanks!谢谢!
The representation you send with a PUT
request should be the same that you expect to get back from a GET
request.您使用PUT
请求发送的表示应该与您期望从GET
请求中返回的表示相同。
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.给定表示的成功 PUT 将表明对同一目标资源的后续 GET 将导致在 200 (OK) 响应中发送等效表示。 -- RFC 7231 -- RFC 7231
In other words, the representation included in the body of a PUT request is expected to be the complete representation.换句话说,包含在 PUT 请求正文中的表示应该是完整的表示。
See also the introduction for HTTP Patch :另请参阅HTTP Patch的介绍:
The existing HTTP PUT method only allows a complete replacement of a document.现有的 HTTP PUT 方法只允许完全替换文档。
Roy Fielding, writing in 2012: Roy Fielding,在 2012 年写道:
When a server that implemented PUT prior to the introduction of Content-Range received a partial content body that included such a range, they would replace the entire resource representation with the partial body.当在引入 Content-Range 之前实现 PUT 的服务器收到包含此类范围的部分内容正文时,他们将用部分正文替换整个资源表示。 That is how PUT was defined to work.... the introduction of partial PUT would only be possible with a strong coupling between client and origin server implementations, which violates the design of the HTTP.这就是 PUT 被定义为工作的方式……只有在客户端和源服务器实现之间存在强耦合时才能引入部分 PUT,这违反了 HTTP 的设计。
There are cases where the representation is very large (much larger than the HTTP headers), and the changes we are making are small, where it would be sensible to send a representation of the changes, rather than the complete representation.在某些情况下,表示非常大(比 HTTP 标头大得多),而我们所做的更改很小,在这种情况下,发送更改的表示而不是完整的表示是明智的。
But PUT
isn't the method we use in that case, because the semantics are wrong.但是PUT
不是我们在这种情况下使用的方法,因为语义是错误的。 Our standards compliant choices here are to end the entire (revised) representation with PUT
, or a representation of the changes in a patch-document with PATCH
.我们在这里符合标准的选择是用PUT
结束整个(修订的)表示,或者用PATCH
表示补丁文档中的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.