I've tried searching this, but I haven't managed to find an answer which would fit my needs.
Considering I currently have the following route:
[GET] /items
Which can be filtered by using query parameters. Now I need to give it the ability to add multiple resources at once. I've considered of doing the following request:
[PATCH] /items
With a body like this:
id[]=1&id[]=2&id[]=3&updateField=newValue
I think there is something wrong with this call, but i'm not able to figure it out.
When building RESTful APIs over HTTP the PUT method is typically used for updating, while POST is used for creating resources.
In this case, we refer to these resources as singleton resources. Collections are themselves resources as well. Collections can exist globally, at the top level of an API, but can also be contained inside a single resource. In the latter case, we refer to these collections as sub-collections.
Yes, You can use POST to update a resource but not using the same URL as the resource you are updating.
Conventionally, updating an object like this via a REST API is done by sending a PATCH or PUT to /people/123 with the new state of the object. However, you could potentially be doing one or many things in your update: Change the person's name. Update an existing address.
In a RESTful API the URL should define the object of the transaction, and the verb the action.
So GET /items
should return all items.
GET /items/1
should return the item with id 1.
It follows that the multiple ids should be part of the resource definition (url). So
GET /items/1,2,3
should return the 3 appropriate items.
Therefore, to apply a partial update to many ids:
[PATCH] /items/1,2,3
Then within the body of the PATCH or PUT, you can provide the information to be updated (assuming you are sending a JSON body).
{"updateField": "newValue"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With