Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement complex queries with a REST api?

I'm building an EmberJS app using ember-data.

Some of the functionality in my app requires quite complex queries.

As an example, let's say I have three entities - students, teachers and classes. If I wanted to get a list of all the students born before 1993 that are taking classes taught by teacher X, how can I do that with a RESTful api? In plain SQL it's easy enough, but I'm unsure of the best practice for implementing this into my API.

Do I need to build a custom endpoint alongside my basic REST api?

So I'd still have:

GET /students (which returns all the students)
GET /students/{id} (which returns a specific student)
etc

But then implement the following for my 'custom' query:

GET /students/custom/born_before/{date}/taught_by/{teacher_id}

Or is there a more standardized way of doing this?

like image 843
Anonymous Avatar asked May 28 '13 15:05

Anonymous


People also ask

How do you pass sensitive data in REST API?

You won't break REST API design by sending a POST in this case. You can send your sensitive data in a HTTP header if that is possible. And ofc. you should use HTTPS if you want to send sensitive data to anywhere.

What are the 4 most common REST API operations?

The most common operations are GET, POST, PUT, PATCH, and DELETE. REST APIs use a stateless request model.

How do I pass multiple query parameters in REST URL?

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.


1 Answers

You can transform your

GET /students/custom/born_before/{date}/taught_by/{teacher_id}

into

GET /students/?born_before={date}&taught_by={teacher_id}

which is just a "query by example" option: you can populate a model instance with the provided fields and make a query using them. The less fields, the broader is the search and more results to show.

This is the way JIRA's API works, for example.

like image 122
Pablo Lozano Avatar answered Oct 18 '22 23:10

Pablo Lozano