views:

685

answers:

3

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.

A: 

I would avoid using GET like parameters. What about this:

http://myapp.com/books/sortedby/author:asc+datepublished:desc/count/12

The idea is that if you need to refine the search for a resource based on a criterion, you add the name of it on the URL, followed by the parameter value. If you need more than one parameter values, separate them with a +.

This is what StackOverflow uses for tagged questions:

http://stackoverflow.com/questions/tagged/java+servlet

I admit however that adding the count/12 at the end looks a bit strange, but I can't think of anything better.

kgiannakakis
What's the reason foravoiding querystring parameters? Putting count, filter, sort, etc. into the URL itself would give me a huge number of URLs to deal with, and it would force people to remember whether sortedby comes before count or vice versa.
dthrasher
+4  A: 

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
bendewey
This is a good way to go as it is also compatible with Solr, a widely-used open-source search engine.
KenE
+1, Plus Comma list is probably better than Comma Comma list.
Dead account
Simple. I like it. We have a winner!
dthrasher
If you use those values directly you'll have a pretty security hole for SQL-injection. I'm warning you ;)
BYK
@BYK, good point, be sure to scrub your results before passing them to SQL, if your planning that.
bendewey
A: 

What about a fully PHP compliant version like this one:

http://myapp.com/books?sort[0][name]=author&sort[0][dir]=asc&sort[1][name]=datepublished&sort[1][dir]=desc&count=12

A bit longer but much convinient and as I've said, compliant with PHP. ASP.NET might have implemented support for this format also.

This will create an array, sort, directly where each element of sort has a name and dir property.

BYK