Denis Kartavenko
Denis Kartavenko

Reputation: 81

tornado.httpclient AsyncHTTPClient() python3

I have a strange problem and i hope somebody encountered with it. I'm working with TelegramAPI and i want to POST file using multipart/form-data. File size 32K

data = {'photo': open('test.jpg', 'rb').read()}

Using simple requests python lib i have no problem:

res = requests.post(url, files=data)

BUT

When i try to use

http_client = httpclient.AsyncHTTPClient() 

http_client.fetch(url, method='POST', body=urllib.parse.urlencode(data))

With the same picture

I got an error

tornado.httpclient.HTTPError: HTTP 413: Request Entity Too Large

I don't know why? requests works fine, but not AsyncHTTPClient, help me please

Upvotes: 1

Views: 1444

Answers (2)

Ben Darnell
Ben Darnell

Reputation: 22134

The body argument in Tornado's HTTP client is similar to the data argument in requests. The files argument is something else entirely: it encodes the file using the multipart encoding. Which one you want to use depends on what format the server is expecting.

In this case the server is expecting multipart encoding, not URL encoding. Tornado does not have built-in support for generating multipart encoding, but as Vitalie said in the other answer, this example code shows how to do it.

Upvotes: 1

Vitalie Maldur
Vitalie Maldur

Reputation: 591

Please check out this demo code. You will see there an example on how to upload files.

Upvotes: 1

Related Questions