k4ppa
k4ppa

Reputation: 4667

How to send a JSON file using a http POST with Tornado?

I'm trying to write a REST service: the client with an http POST request send a JSON to the server, and the server respond with an id.

Basing on this post the code should look like this:

server.py

import tornado.httpclient
from datetime import date
import tornado.ioloop
import tornado.web
from tornado.escape import json_decode, json_encode, url_unescape

class Variazione(tornado.web.RequestHandler):

    def post(self):
        print json_decode(self.request.body)
        response = {'id': '12345'}
        self.write(response)
        tornado.ioloop.IOLoop.instance().stop()

application = tornado.web.Application([
    (r"/variazione", Variazione)
])

if __name__ == "__main__":
    application.listen(8889)
    tornado.ioloop.IOLoop.instance().start()

client.py

import tornado.httpclient
import json
from tornado.escape import json_decode, json_encode
from tornado import gen
import tornado.options

def read_json():
    with open('articoli.json') as json_file:
        json_data = json.load(json_file)
        print json_data
        return json_data

@tornado.gen.coroutine  
def json_fetch(http_client, body):
    response = yield http_client.fetch("http://localhost:8889/variazione", method='POST', body=body)
    raise gen.Return(response)

@tornado.gen.coroutine
def request(http_client):
    data = read_json()
    body = json_encode(data)
    http_response = yield json_fetch(http_client, body)
    print http_response.body


if __name__ == "__main__":
    tornado.options.parse_command_line()
    http_client = tornado.httpclient.AsyncHTTPClient()
    request(http_client)

But nothing happens, the server don't receive anything and no error occur.

Where am I doing wrong?

Upvotes: 0

Views: 7124

Answers (2)

kwarunek
kwarunek

Reputation: 12607

##server.py

First of all, do not stop IOLoop in handler, unless you know what you are doing - in your example after the first request, the application will exit.

So it should look like:

import tornado.ioloop
import tornado.web
from tornado.escape import json_decode, json_encode

class Variazione(tornado.web.RequestHandler):

    def post(self):
        print json_decode(self.request.body)
        response = {'id': '12345'}
        self.write(response)

application = tornado.web.Application([
    (r"/variazione", Variazione)
])

if __name__ == "__main__":
    application.listen(8889)
    tornado.ioloop.IOLoop.instance().start()

##client.py

The request function is a coroutine, so it cannot be called as is. It requires a running IOLoop. The simplest solution in your example is to use run_sync, which will run ioloop, schedule coroutine and will wait until it finished than stop ioloop.

For brevity I've removed part read_json(not related to problem) and move http_client to request.

import tornado.httpclient
import json
from tornado.escape import json_decode, json_encode
from tornado import gen
import tornado.options


@tornado.gen.coroutine  
def json_fetch(http_client, body):
    response = yield http_client.fetch("http://localhost:8889/variazione", method='POST', body=body)
    raise gen.Return(response)

@tornado.gen.coroutine
def request():
    body = '{"test_json": "ok"}'
    http_client = tornado.httpclient.AsyncHTTPClient()
    http_response = yield json_fetch(http_client, body)
    print http_response.body


if __name__ == "__main__":
    tornado.options.parse_command_line()
    tornado.ioloop.IOLoop.instance().run_sync(request)

Upvotes: 2

Ashish Gupta
Ashish Gupta

Reputation: 1239

Do not use this.

class MainHandler(tornado.web.RequestHandler):
    def post(self):
        data = self.get_argument('body', 'No data received')
        self.write(data)

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":

    def handle_request(response):
        if response.error:
            print "Error:", response.error
        else:
            print response.body
        tornado.ioloop.IOLoop.instance().stop()

    application.listen(8888)    
    test = "test data"
    http_client = tornado.httpclient.AsyncHTTPClient()
    http_client.fetch("http://0.0.0.0:8888", handle_request, method='POST', headers=None, body=test)
    tornado.ioloop.IOLoop.instance().start()

USE This:

    import urllib
    post_data = { 'data': 'test data' } #A dictionary of your post data
    body = urllib.urlencode(post_data) #Make it into a post request
    http_client.fetch("http://0.0.0.0:8888", handle_request, method='POST', headers=None, body=body) #Send it off!

Upvotes: 0

Related Questions