Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to specify resource's fields list in RESTful API request

Tags:

rest

I have a RESTful API within a web-service with resources such as users, posts and so on. When I make a request for a list of posts (GET /posts), I want to retrieve an array of posts only with limited data for each post (i.e. subject, author name). When I make a request for a concrete post (GET /posts/42) I want to retrieve the full list of post object fields, including big post body, additional info about likes count, comments count. I suppose there exist many ways to solve this problem.
In my mind, the three most obvious are:

  1. Explicitly specify a fields list on every request (/posts?fields=subject,author_name and /posts/42?fields=subject,body,createAt,author_name,comments_count,likes_count, etc...).
  2. Explicitly specify a fields list only if it differs from the default fields list.
  3. Specify a fields list that should be excluded (or included) from (to) the default fields set if the desired fields set differs from the default.

I want to build a clear and useful API for my customers. Which way should I choose?

like image 501
Ivan Velichko Avatar asked Aug 14 '13 15:08

Ivan Velichko


People also ask

How do I use fields in REST API?

You can create field expressions to use with a set of REST API query methods for Tableau Server resources. The resources are workbooks, data sources, views, and users. Each resource has a set of fields (for example, the name or id of a workbook or owner.

How do I sort data in API?

You can sort data returned from the REST API endpoints. The “sort” query string key is used to order the data that is returned. To sort data in ascending order, enter the desired property name as query string value. To sort data in descending order, simply precede the desired property name with a minus (-) character.

Is there a way to discover all endpoints of a REST API?

There is no way of programmatically discovering REST services as they do not have a standard registry service. Apart from doing something insane brute-force search there is no way of finding the right URLs ( not to mention the right parameters). So the only option is documenting your API.


2 Answers

I'd go for option 2 IMHO.

So if the consumer just requests the resource url (/posts/42) they receive the default fields.

Then consumers can alter the default response by defining values in the query string like:

/posts/42/fields?subject,author_name

This has worked well for me in the past and is how some other well know APIs work, e.g. Facebook

Edit: Looking back at this I’d change the request to be:

/posts/42?fields=subject,author_name

/post/42 is the resource, not fields.

like image 148
Dan Rowlands Avatar answered Oct 21 '22 23:10

Dan Rowlands


Have been doing research into this as well and was pointed towards Facebook's GraphQL as an alternative to requesting a restful api with the fields wanted. It is still in the very early stages but seems very promising.

https://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html

EDIT: Reproduced from the URL:

A GraphQL query is a string interpreted by a server that returns data in a specified format. Here is an example query:

{   user(id: 3500401) {     id,     name,     isViewerFriend,     profilePicture(size: 50)  {       uri,       width,       height     }   } } 

(Note: this syntax is slightly different from previous GraphQL examples. We've recently been making improvements to the language.)

And here is the response to that query.

{   "user" : {     "id": 3500401,     "name": "Jing Chen",     "isViewerFriend": true,     "profilePicture": {       "uri": "http://someurl.cdn/pic.jpg",       "width": 50,       "height": 50     }   } } 
like image 39
ascrookes Avatar answered Oct 22 '22 01:10

ascrookes