2
votes

I'm doing a POST request to an API, the code looks like this:

@gen.coroutine
def call():
    ...
    response = yield AsyncHTTPClient().fetch(
        HTTPRequest(
            url='https://api.mywebsite.com/v1/users',
            headers=headers,
            method='POST',
            body=json.dumps(body),
            validate_cert=False
        )
    )
    print response, response.body

if __name__ == "__main__":
    tornado.ioloop.IOLoop.current().run_sync(call)

The server responds first time with 201 Created and the second time with 200 OK.

But for that code I get this error for the first time. The second time works

Traceback (most recent call last):
  File "t.py", line 49, in <module>
    tornado.ioloop.IOLoop.current().run_sync(call)
  File "/usr/lib/python2.7/dist-packages/tornado/ioloop.py", line 389, in run_sync
    return future_cell[0].result()
  File "/usr/lib/python2.7/dist-packages/tornado/concurrent.py", line 129, in result
    raise_exc_info(self.__exc_info)
  File "/usr/lib/python2.7/dist-packages/tornado/stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 574, in inner
    self.set_result(key, result)
  File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 500, in set_result
    self.run()
  File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 529, in run
    yielded = self.gen.throw(*exc_info)
  File "t.py", line 43, in call
    validate_cert=False
  File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 520, in run
    next = self.yield_point.get_result()
  File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 409, in get_result
    return self.runner.pop_result(self.key).result()
  File "/usr/lib/python2.7/dist-packages/tornado/concurrent.py", line 131, in result
    return super(TracebackFuture, self).result(timeout=timeout)
  File "/usr/lib/python2.7/dist-packages/concurrent/futures/_base.py", line 401, in result
    return self.__get_result()
  File "/usr/lib/python2.7/dist-packages/concurrent/futures/_base.py", line 360, in __get_result
    raise self._exception
AssertionError
1
Why are you using yield in this call? - Ricardo Burillo
Because AsyncHTTPClient.fetch returns a future - alexm92
Could you post more code about the response function? because it seems that the error could come from the yield management... - Ricardo Burillo
Have you initialized your IOLoop? - Ricardo Burillo
Yes it's started. I updated the code - alexm92

1 Answers

0
votes

Looks like you are using sync function HTTPRequest in future


from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPResponse

  def call():
        response = None
        http_client = AsyncHTTPClient()
        try:
            body = json.dumps(body)
            response: HTTPResponse = yield http_client.fetch('https://api.mywebsite.com/v1/users', method='POST', body=str(body), headers=headers, request_timeout=5)
        except Exception as e:
            print('get_request error:{0}'.format(e))