REST URL design for greater than, less than operations
Pagination, greaterthan and lessthan, sound like query parameter to me, since you are queries your resource with these parameters. So you should do something like:
/customers?page=1, or
/customers?page=1>=716, or
/customers?page=1>=716<=819
You can even limit size of page:
/customers?page=1>=716<=819&maxpagesize=100
where gt stands for greater than (same as in xml-escaping) and lt stands for less than.
If you have multiple parameters and need to apply some conditions for each params, I recommend you to pass a JSON object to params.
Consider you want to do a condition for id
and the page
:
/customers?id={"lt": 100, "gt": 30}&page={"start": 1, "size": 10}
It says that I want customers that have Id(s) less than 100 and greater than 30 in the page 1 and page number of 10.
So now simply if you want to apply another condition for other parameters, you can do it by:
/customers?id={"lt": 100, "gt": 30}&children={"lt": 5, "gt": 2}&page={"start": 1, "size": 10}
and this query means customers with Id(s) less than 100 and greater than 30, children less than 5 and greater than 2 in the page number 1 with page size of 10.
I highly recommend you to read this document about designing RESTful API: http://blog.luisrei.com/articles/rest.html
@Julio Faerman:
Well, the problem starts when you get multiple parameters. Imagine the query string for "Customers older than 18 and younger than 60 with more than 2 children".
You can define whatever query parameters you like, such as:
/customers?min-age=19&max-age=59&min-children=3
in my example min and max are integers and are inclusive. You can change that if you prefer. Remember, anything in the URI connotes part of the resource identifier. My personal view is that things after the ?
equate to clauses in the WHERE
part of an SQL query (plus ORDER BY
and LIMIT
, not shown here):
SELECT * FROM customers WHERE age>=19 AND age<=59 AND children>=3
Edit:
Instead of min-
and max-
prefixes, you could allow >
, <
(and maybe !
) as the final character of the parameter name, so instead of min-age
you have a parameter called age>
, which, when combined with a value in the query string, ends up looking like age>=19
:-)
Obviously you can only use this trick when the comparison has an equals sign in it.