REST updating multiple resources

2020-02-11 04:24发布

问题:

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.

回答1:

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"}


标签: rest