开发者

RESTful Web URI Design for Single/Multiple Resource GET [duplicate]

开发者 https://www.devze.com 2023-04-09 21:10 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Rest URL design - multiple resources in one http call
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Rest URL design - multiple resources in one http call

I have looked for answers on this site and the net, but haven't quite figured out the best way to approach this problem.

As an example, say I have the following REST API

Single resource REST API is straightforward:

/car/{id} --> get car by an id

Multiple resource get for a list of cars:

/cars;ids={id1,id2,id3 etc} --> get list 开发者_如何学运维of cars by ids

/cars;list-id={listId};count=5 --> get cars defined by a list id saved in the cloud

/cars;relatives={id};count=5 --> get cars that are related to the car specified by id

A list-id can specify an arbitrary list of elements to return. /cars will just return cars defined in that list.

Note: the matrix params relatives, ids, and list-id cannot all be used in a single GET request. E.g if ids and list-id both appear, then ids will take priority.

Question: Should this be redesigned? And if so, how? Thanks.


What if you view a car as a list ID with just one car? A list ID could then refer to one car or multiple cars. For this to work a list ID and a car ID have to share the same 'domain'. One doesn't take priority over the other because they are actually the same thing - a list of cars.

GET /car/{id} could be one car or a list of cars.

GET /car/{id}/related would return a list of those cars related to the list of cars in the car ID. But this list would have no ID of it's own (yet).

POST /car/{id}/related would return a list ID for the list of cars. That could then be used with GET /car/{id} to return the same list of cars that was also retrieved indirectly with GET /car/{id}/related.

0

精彩评论

暂无评论...
验证码 换一张
取 消