7

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
Lumbendil
  • 2,906
  • 1
  • 19
  • 24

1 Answers1

18

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"}
JDwyer
  • 844
  • 5
  • 8
  • Where you define the list of ids as a wildcard in your route. i.e. routes.Add("/items/{[0-9,]+}", "controller.action") or however the framework that you are using specifies routes. using ASP .NET MVC 3 it would be `context.MapRoute("route_name", "items/{ids}", new { controller = "MyController", action = "MyAction" });` – JDwyer Aug 08 '12 at 21:02
  • I get what you say about `GET /items` returning all the items, but `GET /items?filter=value` can be used to filter such items, right? – Lumbendil Aug 08 '12 at 22:37
  • 1
    correct, you can add any modifiers you want as query sting parameters. `GET /items?sort=name&search=phillips&page=2&itemsPerPage=10&category=tvs&etc=...` – JDwyer Aug 09 '12 at 17:20