Abe Abe - 6 months ago 79
JSON Question

tornado maps GET and POST arguments to lists. How can I disable this "feature"?

The HTTPRequest class in the tornado* web framework helpfully maps GET and POST arguments to lists. I understand why -- in case a given argument name is used multiple times. But for some RequestHandlers, this is a pain. For instance, if I want to pass a json object and parse it as-is on the server.

What's the most straightforward way to disable the map-to-list behavior so that I can send unaltered json to a tornado/cyclone server?

*Cyclone, actually, in case there's an implementation difference here.


Instead of accessing self.arguments directly you should use the accessor functions:

self.get_argument("ID", default=None, strip=False)

This returns a single item.

If you want to turn the arguments into a JSON object you can quite easily do so:

json.dumps({ k: self.get_argument(k) for k in self.request.arguments })