恢复惯例以通过ID或类型和语言更新资源
当前,我按类型和语言的获取请求只能返回一个文档:
GET /documents?type=invitation&language=en
id: 50
text: "I would like to invite..."
更新资源的正确静止约定是什么?
PUT /documents?type=invitation&language=en
?也许我应该仅通过ID更新资源?
PUT /documents/50
Currently my GET request by type and language can return only one document:
GET /documents?type=invitation&language=en
id: 50
text: "I would like to invite..."
What is a correct restful convention to update resource?
PUT /documents?type=invitation&language=en
?? Or maybe I should update resource only by id?
PUT /documents/50
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新整个资源时,通常使用放置。在这里,您只是发送一部分资源数据 - 因此您应该使用补丁程序。
您的URL应该是:
然后您拥有要在请求有效载荷中更新的所有数据:
因此,您现在每个请求仅更新一个对象,并使用适当的方法和资源表示法。
You generally use PUT when you're updating the whole resource. Here you're just sending a subset of resource data - so you should use PATCH.
Your URL should be:
Then you have all the data you want to update in the request payload:
So you're now updating only one object per request and using the appropriate method and resource notation.
通常,您想使用相同的URI要求更改用于检索其副本的资源。
这样做的动机是缓存-HTTP在IT中内置了无效的缓存响应。它不是通用机制,而是旨在处理一些非常具体的情况(基于目标URI,位置字段和内容位置字段的响应无效)。
因此,
应通过该请求进行编辑
对于要发送资源替换表示的请求(考虑“保存文件”)的请求,
。如果您打算仅修改文档的一部分,则可以考虑将补丁文档作为请求正文发送并使用HTTP方法修补程序。
如果您的表示为JSON,则您修改文本的请求可能看起来像:
Normally, you want to use the same URI to request a change to a resource that you use to retrieve a copy of it.
The motivation for this is caching - HTTP has built into it rules for invalidating cached responses. It isn't a general purpose mechanism, but rather designed to handle a few very specific cases (invalidating responses based on target URI, Location field, and Content-Location field only).
Thus
Suggests that edits should be made via
That is a perfectly normal choice for a request that is sending a replacement representation of the resource (think "save file").
If you are intending that the request modify only part of the document, you might consider sending a patch document as the request body and using the HTTP method PATCH.
If your representation were JSON, then your request to modify the text might look like:
这取决于什么
/documents?type =邀请& language = en
回馈。如果这是某种搜索,并且您期望以后多个文档,则put
应该更新所有这些文档,并且由于它是完整的更新,而不是部分更新,因此没有多大意义。在这种情况下,使用补丁
更好,因为它即使在多个资源上也可以进行部分更新。如果此URI是该单个资源的唯一标识符,则可以使用它代替/documents/50
或与之一起使用。您可以为同一资源提供多个标识符。It depends on what
/documents?type=invitation&language=en
gives back. If it is some sort of search and you expect multiple documents later, thenPUT
should update all of those documents and since it is a complete update not a partial, it does not make much sense. UsingPATCH
in this case is better, because it can do partial update even on multiple resources. If this URI is a unique identifier of this single resource, then you can use it instead of/documents/50
or along with it. You can have multiple identifiers for the same resource.