Search code examples
resthttp-patch

REST PATCH for all resources of a given type


I have a set of resources in a REST API, lets say it something like this:

GET /folders
[{ "id": "x", "watched": true }, { "id": "y", "watched": true }, ...]

I've implemented "stop watching" command as a PATCH:

PATCH /folders/x { "watched": false }

What is the right way to implement "stop watching all folders"? I thought of

PATCH /folders { "watched": false }

But I am not sure if this makes sense (the collection itself does not have a watched property).

Or is it something that shouldn't be implemented on API level at all (and instead iterated by client)?
That would seem inefficient though.


Solution

  • I think PATCH /folders { "watched": false } is completely appropriate. Let the implementation decide how the folders will be patched with the given representation of the intended resource state (your JSON). There are no constraints about how the resource representations should look like, they should be self descriptive, so in this case they should have a standard content-type, that's all. (GET responses can be different.)

    I don't recommend you to iterate over the collection by your client, since it is not atomic, it can lose connection at any time. You should send only a single request about this.