Skip to content

Instantly share code, notes, and snippets.

@joshmarshall
Created March 15, 2011 02:43
Show Gist options
  • Save joshmarshall/870225 to your computer and use it in GitHub Desktop.
Save joshmarshall/870225 to your computer and use it in GitHub Desktop.
JSON to Arguments POST in Tornado
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.httputil
import json
class MainHandler(tornado.web.RequestHandler):
def post(self):
# do something useful
name = self.get_argument('foo')
self.write("Hello, %s" % name)
class TestJSONHandler(tornado.httpserver.HTTPParseBody):
def __call__(self):
self.stream.read_bytes(self.content_length, self.parse_json)
def parse_json(self, data):
print data
try:
json_data = json.loads(data)
except ValueError:
raise tornado.httpserver._BadRequestException(
"Invalid JSON structure."
)
if type(json_data) != dict:
raise tornado.httpserver._BadRequestException(
"We only accept key value objects!"
)
for key, value in json_data.iteritems():
self.request.arguments[key] = [value,]
self.done()
application = tornado.web.Application([
(r"/", MainHandler),
])
body_handlers = [
("application/json", TestJSONHandler),
]
if __name__ == "__main__":
application.listen(8888, body_handlers=body_handlers)
tornado.ioloop.IOLoop.instance().start()
# USAGE:
# curl -v http://localhost:8888/ -X POST --data-binary '{"foo": "bar"}' -H "Content-type: application/json"
@ibrahima
Copy link

ibrahima commented Aug 1, 2014

According to https://groups.google.com/forum/#!topic/python-tornado/ZBOsM910RLA this was an experimental feature, not clear if it was ever merged. A shame, as it seems like a nice feature!

Edit: This gist is a nice alternative which does work right now: https://gist.github.com/mminer/5464753

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment