Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST updating multiple resources

Tags:

rest

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.

like image 958
Lumbendil Avatar asked Aug 08 '12 20:08

Lumbendil


People also ask

Which rest method will you use to update a current resource?

When building RESTful APIs over HTTP the PUT method is typically used for updating, while POST is used for creating resources.

What is the recommended term used to refer to multiple resources in API?

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.

Can we update a resource using POST request?

Yes, You can use POST to update a resource but not using the same URL as the resource you are updating.

What is update method in REST API?

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.


1 Answers

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"}
like image 157
JDwyer Avatar answered Sep 30 '22 18:09

JDwyer