PATCH multiple resources
Asked Answered
U

1

6

Short: Is it standard-compliant, RESTful and otherwise good idea to enable PATCH requests to update a collection of resources, not just a single one, but still individually?

Long:

I'm considering exposing a method for enabling batch, atomic updates to my collection of resources. Example:

PATCH /url/myresources
[
    {
        "op": "add",
        "path": "/1",  // ID if the individual resource
        "value": 
        {
            ... full resource representation ...
        }
    },
    {
        "op": "remove",
        "path": "/2"
    },
    {
        "op": "replace",
        "path": "/3/name",
        "value": "New name"
    }
]

The context is a public API of a commercial solution. The benefits of allowing such PATCHes is the atomicity as well as batch-friendliness without spamming requests, handling failures individually etc.

I've consulted https://www.rfc-editor.org/rfc/rfc6902 and https://www.rfc-editor.org/rfc/rfc5789 but couldn't find a definitive answer if this is compliant. The RFCs mostly refer to "a resource", but a collection of resources could also be treated as such.

Is this a good idea? Are there better alternatives?

Unguinous answered 29/1, 2015 at 9:52 Comment(0)
A
2

I like this idea. A collection is a resource, too. So acting on it is perfectly good REST.

The semantic of your PATCH request would be that every subresource not listed in the request body is to be left as it is. Every subresource that is listed is to be changed as described. Yes, that sounds good to me.

As long as every segment of the request can be executed in a single request, I see no problems. Both your "all in one" request and single requests like this would be fine.

PATCH /url/myresources/1
{
    "op": "add",
    "value": 
    {
        ... full resource representation ...
    }
}
Ashram answered 29/1, 2015 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.