I have a RESTful web application that supports multiple sort fields on a collection of items. Is there a common convention for encoding these sort fields into the query string of a URL? I'm considering a pattern like the following:
http://myapp.com/books?sort=author:asc,datepublished:desc&count=12
This would sort the collection of books by author and then by date published.
Basically, I need a convenient way for the name-value pairs in my query string to have name-value pairs of their own. I'll need to do something similar to the above example for filter parameters, too.
Does Rails or ASP.NET MVC have a pattern for this? Are there other frameworks that have established ways for dealing with this issue? I'd rather use a familiar format than roll my own.
I'd also prefer a format that uses as little URL percent-encoding as possible.
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 "&".
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.
Although officially there is no limit specified by RFC 2616, many security protocols and recommendations state that maxQueryStrings on a server should be set to a maximum character limit of 1024.
I've done this before using the standard SQL sorting syntax. There are numerous parsing functions and techniques out there.
http://myapp.com/books?sort=author asc,datepublished desc&count=12
which would be encoded to
http://myapp.com/books?sort=author+asc,datepublished+desc&count=12
For REST, I prefer a more intuitive syntax:
http://myapp.com/books?sort=+author,-datepublished&count=12
And more elaborated (without '+' sign):
http://myapp.com/books?sort=author,-datepublished&count=12
Easy to remember...
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