enderland
enderland

Reputation: 14135

Converting a requests http post to tornado post request

I have the following code:

import requests
s = requests.Session()
r = s.post(AUTHENTICATION_URI, data=form_data, headers=headers)

where form_data and headers are dictionaries of inputs. This returns correctly with a status of 200 using requests and is correct (I am able to use the session later on in my code).

I am attempting to convert this to use tornado and their http client, however when I run this:

from urllib.parse import urlencode
from tornado import httpclient
http_client = httpclient.HTTPClient()

response = http_client.fetch(
    httpclient.HTTPRequest(
    AUTHENTICATION_URI, method='POST', request_timeout=60, body=urlencode(form_data), headers=headers))

I am running into a timeout:

tornado.httpclient.HTTPError: HTTP 599: Timeout during request

The tornado HTTPRequest object has an optional timeout parameter, but even making that 60 seconds doesn't seem to resolve the issue.

What do I have to do differently in how I construct the tornado post request in order to recreate how I was running this using the requests http module?

Upvotes: 0

Views: 451

Answers (1)

Ben Darnell
Ben Darnell

Reputation: 22134

http_client.fetch does not return a response, it returns a Future that resolves to a response. You need to call it in an async def function with with response = await http_client.fetch(...). You must also have started the IOLoop, and not be doing anything to block it. If none of this points you in the right direction, you'll need to share a more complete code sample.

Upvotes: 1

Related Questions