Walery Strauch Walery Strauch - 1 month ago 9
HTTP Question

REST Array manipulation best practice

I have full access to foo resource via REST:

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


I would like to delete
tag01
in
tags
array.

Usually I would
GET \foo
and
PUT \foo
it back without
tag01
.
In this case this object is small, so this is ok.

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.

My request in PATCH way is now

PATCH /foo/tags?op={add|delete}


To delete I would use:

PATCH /foo/tags?op=delete


With this data:

{
"value": "tag01"
}


There are now two thinks that I don't like:


  • 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



It doesn't look correct to me.

Is there some other way to manipulate arrays via REST?

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

Answer

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.

For instance you could send:

PATCH /foo

[
  { 
    "op": "remove",
    "path": "/tags/0" 
  }
]

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

See the JSON Patch draft for more details.