Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST api: requesting multiple resources in a single get [duplicate]

Tags:

rest

I'm trying to design a RESTful API where the users can fetch a single product or list of products in a single GET request. Each product has a unique id.

The single product URL is simple enough:

http://mycompany.com/api/v1/product/id 

This returns the information for a single product. I'm confused as to how the URL for multiple product information should look like.

How about

http://mycompany.com/api/v1/product/ids 

where ids is a comma separated list of ids?

like image 319
user824212 Avatar asked Feb 21 '12 02:02

user824212


People also ask

How REST API handle multiple requests?

Using multiple threads: The REST API can use multiple threads to handle multiple client requests. This means that each request will be processed on a separate thread, and the REST API can process multiple requests at the same time. Using a queue: The REST API can use a queue to store incoming requests from clients.

How do I avoid the same request for multiple times to the server?

You can't stop the user from resubmitting a new request with the same ticket, but you can reject it on the server side, with a "Duplicate request" error. Right, but people do that on purpose - submit a form, press back because they know they need to correct something, submit form again.

Is it possible to have multiple representations for the same resource in REST?

You could make them 2 representations of the same resource and with content negotiation.. 100% restful.

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.


1 Answers

I would recommend thinking of it like you are listing multiple representations of the resource filtered by id. As such you make a GET request to the base resource:

https://example.com/api/v1/products

And filter the response list by id:

https://example.com/api/v1/products?id=1,2,3

like image 176
abraham Avatar answered Sep 22 '22 15:09

abraham