In the post method of a RequestHandler in Tornado, self.get_argument(arg, None) gets the value of arg contained in the request.
All the examples I’ve seen return a string as value, which makes me wonder if we can send data other than strings in a POST request.
The specific use case I am interested in: I have a form with multiple fields on the page, and I want to pack a subset of its fields together in a single argument in the POST request to the server, something like
packed = self.get_argument('packed_arg', None)
arg1 = packed['arg1']
arg2 = packed['arg2']
...
while the rest of the fields are still of individual arguments. So far I’m thinking data structure similar to a dict (maybe JSON?) for this packed argument, is this feasible?
JSON will fit well for your purposes.
Do something like this, on client side:
Function
get_form_args()is abstraction. You can implement it any way. Javascript objects are JSONs by default.So on client side you must create dictionary from form fields.
Think this way:
And then on server side:
Also you can access all POST args in
self.request.arguments.