I am having a bit of difficulty designing a url for a rest service that can handle requests for customers based on pagination as one type of operation or requesting greater than or less than operators as another type of operation. For example:
Pagination:
GET /customers/0/100
This will get 100 customers for page 0.
Greater/Less Than:
I also need a URL design to get customers that have an id greater than n (e.g. lets say 716). How would you incorporate "greater than" or "less than" in a url. I have to bear in mind that characters ">" and "<" are illegal in urls. I think this url design looks odd:
GET /customers/greaterthan/716
GET /customers/lessthan/716
I can't use a range as that would conflict with the pagination pattern specified above and is not a nice solution in any case e.g.:
GET /customers/716/999999999999
GET /customers/0/716
I'm sure that I'm missing something obvious - does anyone have a better solution?
Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".
Although URLs containing parameters within the query string do themselves conform to REST constraints, the term “REST-style URL” is often used to signify a URL that contains its parameters within the URL file path, rather than the query string.
Our recommendation is to avoid using spaces in URLs, and instead use hyphens to separate words. If you are unable to do this, make sure to encode whitespace using "+" or "%20" in the query-string, and using "%20" within the rest of the URL.
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
Here is how I have been doing it.
Let's say you have the field age which is a number.
This is what the urls would look like
Equals: /filter/age=5
Greater Than: /filter/age[gt]=5
Greater Than Equals: /filter/age[gte]=5
Less Than: /filter/age[lt]=5
Less Than Equals: /filter/age[lte]=5
Not Equals: /filter/age[ne]=5
Then when I pass these arguments to the back-end I have a script that just parses the key out and converts it to the correct filter based on the age[INSERT_OPERATOR_HERE]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With