简体   繁体   English

REST api PUT 是否应该接受具有单个编辑属性和文档 ID 的请求?

[英]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.

相关问题 单个休息'put'端点是否足以使用嵌套数组更新文档? - Is single rest 'put' endpoint enough to update a document with nested arrays? 我应该将用户 ID 放在 REST url 中还是从身份验证中获取 - Should I put a user id in a REST url or get it from authentication 单个POST请求中执行多项操作-REST API - multiple operations in a single POST request -REST api 节点 rest API:放置请求未更新请求数据 - Node rest API: put request is not updating the request data NodeJS API:在Mongodb中默认通过“ Id”属性(而不是通过“ _id”)将文档查找到集合中 - NodeJS API: Find a document into a collection by “Id” property , not by “_id” default in Mongodb 向在Postman中工作的REST API发出PUT请求,但在使用Fetch时不起作用 - PUT request to REST API working in Postman but not when using Fetch PUT请求中的错误(带有NodeJs API REST的Angular 4) - ERROR in PUT request (Angular 4 w/ NodeJs API REST) 当属性在 REST api 中没有值时应该返回什么? - What should be returned when a property does not have a value in a REST api? 我应该在参数中发送所需的 ID 还是作为请求 object 中的属性发送? - Should i send the needed id in the params or as property in the request object? 没有 id 的 PUT 请求 - PUT request without id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM