REST 数组操作最佳实践 [英] REST Array manipulation best practice

查看:34
本文介绍了REST 数组操作最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过 REST 完全访问 foo 资源:

I have full access to foo resource via REST:

{
  "name": "foo",
  "tags": [
    "tag01",
    "tag02",
    "tag03"
  ]
}

我想删除tags数组中的tag01.

通常我会在没有 tag01 的情况下 GET \fooPUT \foo 返回它.在这种情况下,这个对象很小,所以没问题.

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:

  • query field op - rfc 或 smth 中是否有一些默认名称.像这样
  • 请求数据中的
  • member value - 这也是自由选择的名称
  • 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?

是否有一些命名约定以 PATCH 方式实现?

Are there some name conventions to do it in PATCH way?

推荐答案

PATCH 的负载应该包含 说明应如何修改当前驻留在源服务器上的资源以生成新版本的说明".所有信息都应在有效负载中传递,而不是在查询参数中传递.

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" 
  }
]

Path /tags/0 指向数组的第一个元素.其余元素应向左移动.

Path /tags/0 points to the first element of the array. The remaining elements should be shifted to the left.

查看 JSON 补丁草案了解更多详情.

这篇关于REST 数组操作最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆