Json-api: Updating Multiple Resources

Created on 23 Apr 2014  路  5Comments  路  Source: json-api/json-api

Trying to work out the best way to implement the update action in my app, and wasn't clear on the best way to do this.

So the format says we should "Update URLs are determined the same way as GET URLs." And it also shows examples of performing those GETs with multiple IDs like http://example.com/comments/1,2,3,4

Should I do the same thing with an update action, and simply map the IDs in the URL to the JSON in the PUT body?


So the request might be:

PUT http://example.com/comments/1,2,3

With this body (map ID to the position in the array)

{
  "comments": [
    {
      "id": "1",
      "body": "Mmmmmakase"
    }, {
      "id": "2",
      "body": "I prefer unagi"
    }, {
      "id": "3",
      "body": "What's Omakase?"
    }
]}

The other option is to include the ID in the body of the JSON and ignore any IDs in the URL.

So the request might be:

PUT http://example.com/comments

{
  "comments": [
    {
      "id": "1",
      "body": "Mmmmmakase"
    }, {
      "id": "2",
      "body": "I prefer unagi"
    }, {
      "id": "3",
      "body": "What's Omakase?"
    }
]}

Is there a recommended approach while doing the full update with PUT?

All 5 comments

PUT is intended for replacing or creating, I think PATCH is the preferred method for updating. Although it is not explicitly stated in the specification, I believe a bulk PATCH request may be allowed:

PATCH http://api.example.com/balls/5,7,11
[
  { "path": "/balls/0/color", "op": "replace", "value": "blue" },
  { "path": "/balls/1/color", "op": "replace", "value": "green" },
  { "path": "/balls/2/color", "op": "replace", "value": "red" }
]

In this case, the index in the path corresponds to the order that the IDs appear, since doing a GET on the route would return entities in that order. This is all speculative on my part and it's not clear how bulk updates should be handled.

@daliwali in this case I was using PUT to mean replace.

This was my attempt to be as JSON APIy as I can with the time I have 馃榿 so instead of fully implementing the preferred PATCH format I was hoping to fit this into a rails app in a reasonable way.

Bulk updates are now covered by both the Bulk extension and the Patch extension.

Extensions are now removed?

Yes, what should we do now for bulk apis?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Niklas9 picture Niklas9  路  11Comments

masterspambot picture masterspambot  路  6Comments

jfinkels picture jfinkels  路  15Comments

Ilyes512 picture Ilyes512  路  8Comments

designermonkey picture designermonkey  路  13Comments