Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to design a REST API with multiple filters?

Tags:

rest

mysql

api

As a personal programming project, I am working on scraping my University's course catalog and providing the data as a REST API. I have successfully scraped all the data and stored it in a database, and am now working on the API.

The courses can be filtered on the basis of many criteria: instructor, college, credits, time, day etc.

What is the best way to provide an API in this situation?

Option 1

Provide numerous URLs such as

example.com/api/byinstructor/<instructorcode>
example.come/api/bycollege/<collegecode>
example.com/api/bycollegeandinstructor/<collegecode>/<instructorcode>
...and so on

I would need to have a URL for all permutations. This seems very cumbersome, both for me and the API consumers, and very un-DRY.

Option 2

Only provide APIs for the major options like:

example.com/api/byinstructor/<instructorcode>
example.come/api/bycollege/<collegecode>

And if the consumer wants bycollegeandinstructor, he does the filtering on his end.

Option 3

The user passes a JSON string to me, and I use that to get the filtering criteria

example.com/api/getcourses/<jsonstring>

jsonstring = 
{ 
  instructor:<instructorcode>,
  college:<collegecode>,
  ...and so on
}

I suppose instead of the Json string, I could also require a POST array, but that seems un-inituitive for the consumer since he is GETing data.

Or is there another way of doing this I am not aware of? If it is the third option that is the best option, could you provide a short summary best to prepare a SQL query on the basis of a JSOn string that may have variable number of values?

like image 394
xbonez Avatar asked Jan 10 '12 16:01

xbonez


People also ask

What are the 4 most common REST API operations?

These operations stand for four possible actions, known as CRUD: Create, Read, Update and Delete. The server sends the data to the client in one of the following formats: HTML. JSON (which is the most common one thanks to its independence of computer languages and accessibility by humans and machines)

Should I use plural in REST API?

Plural Nouns Are Preferable, Rather Than Singular Using plural or singular nouns for defining resources has no any impact on how your API will work; however, there are common conventions that are used in all good RESTful APIs. One of such conventions is the use of plural nouns for defining resources.


2 Answers

To expand on the answer from J.F., it sounds like you have one resource, the set of courses, which would be at the URI:

/courses

Filtering that resource is usually accomplished using query parameters to filter that single resource, e.g:

/courses?college=123&instructor=321

By doing this, you avoid the issue with all possible permutations creating a proliferation of resources.

Fundamentally: There's one resource, which can be filtered as necessary.

like image 78
Pete Avatar answered Oct 30 '22 16:10

Pete


GET example.com/courses?college=<collegecode>&instructor=<instructorcode>
like image 21
jfs Avatar answered Oct 30 '22 17:10

jfs