views:

272

answers:

3

An HTTP GET query string is a ordered sequence of key/value pairs:

?spam=eggs&spam=ham&foo=bar

Is, with certain semantics, equivalent to the following dictionary:

{'spam': ['eggs', 'ham'], 'foo': bar}

This happens to work well for boolean properties of that page being requested:

?expand=1&expand=2&highlight=7&highlight=9
{'expand': [1, 2], 'highlight': [7, 9]}

If you want to stop expanding the element with id 2, just pop it out of the expand value and urlencode the query string again. However, if you've got a more modal property (with 3+ choices), you really want to represent a structure like so:

{'highlight_mode': {7: 'blue', 9: 'yellow'}}

Where the values to the corresponding id keys are part of a known enumeration. What's the best way to encode this into a query string? I'm thinking of using a sequence of two-tuples like so:

?highlight_mode=(7,blue)&highlight_mode=(9,yellow)

Edit: It would also be nice to know any names that associate with the conventions. I know that there may not be any, but it's nice to be able to talk about something specific using a name instead of examples. Thanks!

+6  A: 

The usual way is to do it like this:

highlight_mode[7]=blue&highlight_mode[9]=yellow

AFAIR, quite a few server-side languages actually support this out of the box and will produce a nice dictionary for these values.

Konrad Rudolph
Please elaborate on "quite a few".
Crescent Fresh
Well, if I had more details, I would have mentioned them. I *don't know* where this is supported but I remember seeing it in a lot of places. 'taw' mentioned Ruby on Rails above. Many other frameworks will probably copy this and I've already used it in PHP.
Konrad Rudolph
+3  A: 

I've also seen people JSON-encode the nested dictionary, then further encode it with BASE64 (or something similar), then pass the whole resulting mess as a single query string parameter.

Pretty ugly.

On the other hand, if you can get away with using POST, JSON is a really good way to pass this kind of information back and forth.

Brian Clapper
+2  A: 
taw
Very informative background info -- thanks!
cdleary