REST数组操作的最佳实践 [英] REST Array manipulation best practice
问题描述
我已经通过REST完全访问foo的资源:
I have full access to foo resource via REST:
{
"name": "foo",
"tags": [
"tag01",
"tag02",
"tag03"
]
}
我想删除 tag01
在标记
阵列。
通常我会 GET \\富
和 PUT \\富
回不 tag01
。
在这种情况下,这个对象是小的,所以这是确定的。
Usually I would GET \foo
and PUT \foo
it back without tag01
.
In this case this object is small, so this is ok.
但是让我们假设它是更大的。对于这种情况下,我不喜欢下载和上传这些数据。一些谷歌的研究后,我发现了 HTTP PATCH 。我看起来像正是我需要的。
But let's assume it's much bigger. For this case I don't like to download and upload this data. After some google research I found out http PATCH. I looks like exactly what I need.
我的PATCH方式的要求是现在
My request in PATCH way is now
PATCH /foo/tags?op={add|delete}
要删除我会用:
PATCH /foo/tags?op=delete
通过这样的数据:
{
"value": "tag01"
}
现在有两个认为我不喜欢的:
There are now two thinks that I don't like:
- 查询字段
运
- 是否有在RFC或水木清华所描述的一些deafult名字。像这样 - 成员
值
中请求数据 - 这也是自由选择的名称
- query field
op
- are there some deafult names described in rfc or smth. like this - member
value
in request data - this is also freely chosen name
这看起来不正确我。
有一些其他的方式来操纵通过REST阵列?
Is there some other way to manipulate arrays via REST?
是否有一些名称约定做补丁的方式?
Are there some name conventions to do it in PATCH way?
推荐答案
在 PATCH的有效载荷
应包含的说明描述如何目前居住在原始服务器上的资源应该进行修改,以产生一个新的版本一>。所有信息应在有效载荷传递,而不是在查询PARAMS。
The payload of a PATCH
should contain "instructions describing how a resource currently residing on the origin server should be modified to produce a new version". All information should be passed in the payload and not in query-params.
例如,你可以发送:
PATCH /foo
[
{
"op": "remove",
"path": "/tags/0"
}
]
路径 /标签/ 0
指向数组的第一个元素。剩余的元素应当左移
Path /tags/0
points to the first element of the array. The remaining elements should be shifted to the left.
请参阅有关详细信息, JSON补丁草稿
See the JSON Patch draft for more details.
这篇关于REST数组操作的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!