Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced Queries in REST

Tags:

rest

I'm trying to create a more advanced query mechanism for REST. Assume I have the following:

GET  /data/users

and it returns a list of users. Then to filter the users returned for example I'd say:

GET /data/users?age=30

to get a list of 30 year old users. Now lets say I want users aged 30 - 40. I'd like to have essentially a set of reusable operators such as:

GET /data/users?greaterThan(age)=30&lessThan(age)=40

The greaterThan and lessThan would be reusable on other numeric, date, etc fields. This would also allow me to add other operators (contains, starts with, ends with, etc). I'm a REST noob so I'm not sure if this violates any of the core principles REST follows. Any thoughts?

like image 757
NorthFork Avatar asked Nov 04 '22 07:11

NorthFork


2 Answers

Alternately, you might simply be better off with optional parameters "minAge" and "maxAge".

Alternative 2: encode the value(s) for parameters to indicate the test to be performed: inequalities, pattern matching etc.

This gets messy no matter what you do for complex boolean expressions. At some point, you almost want to make a document format for the query description itself, but it's hard to think of it as a "GET" anymore.

like image 172
Roboprog Avatar answered Dec 03 '22 02:12

Roboprog


I would look into setting the value of the query parameter to include syntax for operators and such .. something like this for a range of values

/data/users?age=[30,40]

or

/data/users?age=>30&age=<40

would make it a little easier to read, just make sure to url encode if you are using any reserved characters

like image 31
Chad Brown Avatar answered Dec 03 '22 02:12

Chad Brown