Fougere
Fougere

Reputation: 197

REST Field filter use

I'm designing a RESTful API and I'm asking myself question about the filter field.

On my gets queries I want the user to be able to select the fields he want to get in the response. I was pretty sure that it would be the field filter jobs to give me the requested field but, after some reshearch, I found that most of the time it's used to add criteria on the fields, as a IF. Is it the user that needs to make show or hide the fields ans the Api return the full ressource everytime ?

I got an other question which is about the URI representation of such filter. Should it be something like /foo?fields=[bar1,bar2] ?

Thanks

Upvotes: 1

Views: 5118

Answers (3)

Corby Page
Corby Page

Reputation: 1405

Yoga is a framework that allows you to deploy your own REST API's with selectable fields. This can reduce roundtrips to the server, and improve performance.

Upvotes: 0

botchniaque
botchniaque

Reputation: 5084

Google Compute Engine API uses the 'fields' request parameter (see the documentation). The syntax is flexible enough to let user select/restrict even the nested elements. You may find it useful.

Upvotes: 1

gitaarik
gitaarik

Reputation: 46270

It's not common to have a resource where you can specify what fields you want returned, by default all fields will get returned. If your resource has a lot of fields or some fields have really big values, it can be a good idea to have a way to specify which fields you want returned.

In REST there are no strict rules about how you should design your URLs for filters. It is indeed common to use GET parameters because they can be optional and don't have to be in any specific order. Your proposal of /foo?fields=[bar1,bar2] seems fine, however i would personally leave off the brackets.

Upvotes: 3

Related Questions