Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado request.body

My Tornado application accepts POST data through http body request

In my handler I am able to get the request

def post(self):     data = self.request.body 

The data I am getting is in the from of str(dictionary)

Is there a way to receive this data in the form of a Python dictionary?

I don't want to use eval on the server side to convert this string to a Python dictionary.

like image 410
Joel James Avatar asked May 08 '13 23:05

Joel James


People also ask

How many requests can tornado handle?

Python Tornado Request Performance.md Meaning tornado will be able to handle a maximum of 10 simultaneous fetch() operations in parallel on each IOLoop. This means a single tornado process should only be able to handle ~3 incoming requests before subsequent ones would queue in Tornado's AsyncHTTPClient.

What is Tornado web application?

A Tornado web application generally consists of one or more RequestHandler subclasses, an Application object which routes incoming requests to handlers, and a main() function to start the server.


2 Answers

As an alternative to Eloim's answer, Tornado provides tornado.escape for "Escaping/unescaping HTML, JSON, URLs, and others". Using it should give you exactly what you want:

data = tornado.escape.json_decode(self.request.body) 
like image 107
Farray Avatar answered Oct 06 '22 00:10

Farray


You are receiving a JSON string. Decode it with the JSON module

import json  def post(self):     data = json.loads(self.request.body) 

For more info: http://docs.python.org/2/library/json.html

like image 38
Eloims Avatar answered Oct 05 '22 23:10

Eloims