python - Tornado application/json support -
python - Tornado application/json support -
does tornado back upwards content-type "application/json"
?
according phone call stack (assuming stream_request_body = false
), method called parse request body parse_body_arguments (httputil.py 662), accepts "application/x-www-form-urlencoded"
, "multipart/form-data"
the solution pretty trivial. have json.loads()
received body , trust it's proper json-encoded dictionary (if want, grab exception , provide meaningful feedback). can't expect application/json
in content-type
; during post that'll application/x-www-form-urlencoded
.
here sample server:
import json import tornado.httpserver import tornado.ioloop import tornado.web class myhandler(tornado.web.requesthandler): def post(self): info = json.loads(self.request.body.decode('utf-8')) print('got json data:', data) self.write({ 'got' : 'your data' }) if __name__ == '__main__': app = tornado.web.application([ tornado.web.url(r'/', myhandler) ]) http_server = tornado.httpserver.httpserver(app) http_server.listen(8888) print('starting server on port 8888') tornado.ioloop.ioloop.instance().start()
you can test using e.g. curl
:
curl -d '{"hello": "world"}' http://localhost:8888/
python json tornado
Comments
Post a Comment