Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL query string convention for multiple sort

Tags:

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.

like image 690
dthrasher Avatar asked Feb 23 '09 16:02

dthrasher


People also ask

How do I pass multiple path parameters in URL?

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 "&".

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.

How many query string parameters can be added into a URL?

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.


2 Answers

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
like image 93
bendewey Avatar answered Sep 18 '22 06:09

bendewey


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...

like image 30
joseaio Avatar answered Sep 19 '22 06:09

joseaio