6

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.

Tito
  • 832
  • 10
  • 24

1 Answers1

4

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.

Aleksandar Stojadinovic
  • 4,851
  • 1
  • 34
  • 56
  • Thanks, looks like a good way to do it. I'm wondering - would it ever make sense not to include the ID? (i.e. should I always force it as a field?) – Tito Feb 23 '15 at 19:03
  • I guess that really depends on your use case and the meaning of the ID. If the client is expected to reference that resource again soon, I would include the ID always. If not, than you don't need to. – Aleksandar Stojadinovic Feb 23 '15 at 19:06