恢复惯例以通过ID或类型和语言更新资源

发布于 2025-02-01 07:43:08 字数 308 浏览 2 评论 0原文

当前,我按类型和语言的获取请求只能返回一个文档:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时间你老了 2025-02-08 07:43:08

更新整个资源时,通常使用放置。在这里,您只是发送一部分资源数据 - 因此您应该使用补丁程序。

您的URL应该是:

PATCH /documents/{id}

然后您拥有要在请求有效载荷中更新的所有数据:

{
   "type": "invitation",
   "language": "en"
}

因此,您现在每个请求仅更新一个对象,并使用适当的方法和资源表示法。

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:

PATCH /documents/{id}

Then you have all the data you want to update in the request payload:

{
   "type": "invitation",
   "language": "en"
}

So you're now updating only one object per request and using the appropriate method and resource notation.

暮凉 2025-02-08 07:43:08

什么是正确更新资源的正确的恢复约定?

通常,您想使用相同的URI要求更改用于检索其副本的资源。

这样做的动机是缓存-HTTP在IT中内置了无效的缓存响应。它不是通用机制,而是旨在处理一些非常具体的情况(基于目标URI,位置字段和内容位置字段的响应无效)。

因此,

GET /your-resource-identifier-here

应通过该请求进行编辑

POST /your-resource-identifier-here
PUT /your-resource-identifier-here
PATCH /your-resource-identifier-here

PUT /documents?type=invitation&language=en

对于要发送资源替换表示的请求(考虑“保存文件”)的请求,

。如果您打算仅修改文档的一部分,则可以考虑将补丁文档作为请求正文发送并使用HTTP方法修补程序。

如果您的表示为JSON,则您修改文本的请求可能看起来像:

PATCH /documents?type=invitation&language=en
Content-Type: application/json-patch+json

[{
 "op": "replace",
 "path": "text",
 "value": "your new text here"
}]

What is a correct restful convention to update resource?

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

GET /your-resource-identifier-here

Suggests that edits should be made via

POST /your-resource-identifier-here
PUT /your-resource-identifier-here
PATCH /your-resource-identifier-here

PUT /documents?type=invitation&language=en

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:

PATCH /documents?type=invitation&language=en
Content-Type: application/json-patch+json

[{
 "op": "replace",
 "path": "text",
 "value": "your new text here"
}]
她如夕阳 2025-02-08 07:43:08

这取决于什么/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, then PUT should update all of those documents and since it is a complete update not a partial, it does not make much sense. Using PATCH 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文