3

I'm trying to pass lists and maps as GET parameters, and I'm wondering if there are particular conventions (or preferably, libraries) with which I can do so. Specifically, I'd like to pass lists and maps.

Lists are relatively easy, given that we properly URL-encode our delimiter (in this case, a comma):

/images/kittens?furColors=brown,black

Maps are more complicated. I've seen something like this:

/images/kittens?properties[furColors]=brown,black&properties[eyeColors]=blue

I've also seen something like this:

/images/kittens?properties=furColors%3Dbrown,black;eyeColors%3Dblue;

Is there a nice standard somewhere that I can follow? Also, I happen to be using Java and Spring (for reasons out of my control), and I'd like to be able to use a library to handle this, preferably one whose strings Spring can parse.

spitzanator
  • 1,877
  • 4
  • 19
  • 29
  • 3
    Be careful when using GET requests, I don't know how big are your maps, but links below might be useful for you: http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url and http://www.boutell.com/newfaq/misc/urllength.html – andbi Mar 19 '12 at 22:28

2 Answers2

6

You could pass JSON arrays or objects. Why not use an existing, well established standard rather than inventing your own?

Or perhaps protobuf would be a better fit.

I'd rather do anything that develop and maintain my own. I'm lazy.

Watch out for the 1024 byte limit on GET request strings. You can only be so complex.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • JSON is really verbose for what I'm trying to do. The extraneous quotes around keys and the enclosing brackets are really redundant. – spitzanator Mar 19 '12 at 22:18
  • Still better than making up your own protocol and parsers and maintaining them, in my view. – duffymo Mar 19 '12 at 22:46
  • What about protobuf? See above. – duffymo Mar 19 '12 at 22:48
  • protobuf is a binary protocol, no? I suppose I could protobuf and base64 encode, but I'd like the URLs to be somewhat readable, too. – spitzanator Mar 19 '12 at 23:11
  • Jeez, you're hard to please.... 8) Guess you'd better go ahead and write your own. – duffymo Mar 20 '12 at 01:02
  • Turns out, Spring won't accept [ or ] in the value of a GET parameter. It converts them to spaces. What the hell? In any event, good response! I will end up writing my own, though... – spitzanator Mar 20 '12 at 05:47
  • Maybe Spring will accept them, you just need to encode them properly on the client and decode them using a filter or aspect on the server. – duffymo Mar 23 '12 at 18:32
0

Taking a page from the google commerce search api, specifically the restriction section where they basically pass a map of restrictions such as

&restrictBy=key1=val1,key2=val2,key3=val3

which is very similar to your options. I'm not aware of any published best practice or standard, and I'm not even convinced that Google's endorsement carries the same weight it used to, but at least it's something that has been done and is in production.

As for a library, using a couple of well-timed String.split would work, or you could look at Guava's MapSplitter.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115