Reputation: 3076
I would like to know if there is RESTful way to handle updating multiple records with one request where you don't know the ids of the records to update.
Example:
You have resource /jobs
. Say you are able list these jobs in the following way GET /jobs?sort=priority&inprogress=false&limit=10
This will return you a list of 10 jobs that are not in progress in priority order.
Now say you want change the state of those jobs to inprogress=true. What would be a RESTful way of achieving this? Obviously you could perform a GET and then update each of those individually but you may have another process that has already grabbed those between you doing the GET and making the update. Then you'll get a failed request because those have already been updated, which is the main thing I want to avoid.
Would it be RESTful to perform an update based on filtering like a GET request e.g
PATCH /jobs?sort=priority&inprogress=false&limit=10
{[op:"replace", path:"/inprogress", value:true]}
So that this would update the first 10 jobs that are not in progress in priority order, changing them to be inprogress and returning them.
Is this RESTful? Or is there a better way to achieve this in a RESTful way?
Upvotes: 0
Views: 1635
Reputation: 2421
Update:
After reading the comments I realize that the command to update the resources really is PATCH, so this answer do not applies. Sorry for the confusion.
In Rest definition GET (and also PUT) operations must be idempotent : it means that (see here)
"the side-effects of N > 0 identical requests is the same as for a single request:
(so when using idempotent operations your client application could repeat any call with the confidence it will not have unexpected results)
The way you pretend to use GET clearly does not follows this restriction, you must use a POST to get this "stopped" processes.
Also I would use some different resource to clearly differentiate this operation: something like
POST /activate-jobs?sort=priority&limit=10
Upvotes: 1