0

Is there a standard API call (or set of calls) that allows me to encode a list of key/value pairs into a SINGLE request parameter? Somehow I have never done this before.

What I am looking for is to generate a URL which encodes an arbitrarily long string of parameters that will be decoded by the indicated page. For example:

http://someth.ing/page?params=xxxxxxxxxxxxx

where the xxxxxxx is the encoded form of a

Map<String, String>

or if that isn't available then a

List<String>

I know my alternative is to just encode each key as its own request parameter, but that creates a lot of parsing work on the decoding side I would rather avoid. Any suggestions?

AlanObject
  • 9,613
  • 19
  • 86
  • 142
  • 1
    There is no standard. To automate things, I would design a prefix and add it to the name of the keys as params (something like '?mymap_key1=value1&mymap_key2=value2...`. Retrieving all parameters, checking for the prefix and rebuilding the map can be done by a helper routine that can be reused. – SJuan76 Oct 14 '12 at 15:05

1 Answers1

1

You could just convert to/from JSON. You could use among others Google Gson for this:

Map<String, String> data = createItSomehow();
String dataAsJson = new Gson().toJson(data);
String url = "http://example.com/some?data=" + URLEncoder.encode(data, "UTF-8");
// ...

And in the other side, assuming a servlet:

String dataAsJson = request.getParameter("data");
Map<String, String> data = new Gson().fromJson(json, new TypeToken<Map<String, String>>(){}.getType());
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This is an interesting idea I hadn't considered. A big advantage of using JSON is that it would make processing on the client side (if I ever wanted to) much easier. I'll play with it over the next couple of days and report back. – AlanObject Oct 14 '12 at 16:54