Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST - Getting specific property of resources in collection only

I'm developing the search functionality of my REST API and currently URI's structured as:

api/items?type=egg,potato

Let's say each item resource has 4 properties:

Id, Name, Type, Rating

What would be the most restful way to design my URI and return a subset of properties of each resource, e.g. only names of these resources?

--

The reason I ask this is I often want a less heavy duty result-set. For instance, I may build an AJAX search with dynamically populated names as dropdowns - but I do not want extra bloat coming back with each request.

like image 966
Titus Avatar asked Oct 19 '22 16:10

Titus


1 Answers

REST isn't really a set of rock-solid standards, but there are some nice practices.

In this particular case I would recommend using the query parameter of an existing resource field as you are now, to select items which have the type value of egg or potato. But to select only a subset, you can introduce a field query parameter. So you can call your API like api/items?type=egg&fields=name, to get only the name field of all the resources with the egg type.

P.S This is not my invention, I already seen this in other API's, somewhere called select. As far as I know, Facebook has this feature in its API's.

like image 157
Aleksandar Stojadinovic Avatar answered Oct 23 '22 00:10

Aleksandar Stojadinovic