What makes the 'Request Entity Too Large' error when sending an image to Telegram?

Question:

File size: 51.2 KB
Trying to send:

>>> send_img_url = 'https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXX/sendPhoto'
>>> img_name = 'C:/Users/Administrator/Downloads/WhatsApp Image 2019-05-30 at 20.54.40.jpeg'
>>> r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': open(img_name, 'rb')})
>>> r
<Response [413]>
>>> r.reason
'Request Entity Too Large'
>>> r.content
b''
>>>

Also i try some another requests like:

photo = open(('C:/Users/Administrator/Downloads/WhatsAppImage.jpeg').encode('utf-8'), 'rb')
r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': photo})

and:

 with io.open('C:/Users/Administrator/Downloads/WhatsAppImage.jpeg', encoding='utf-8', errors='ignore') as f:
    r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': f})

Last option give me next error:

>>> r
<Response [400]>
>>> r.reason
'Bad Request'
Asked By: kshnkvn

||

Answers:

You’re probably doing it wrong.

As Bot API docs says:

Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files.

In requests lib, by using data= keyword argument, you’re sending payload using form-encoded type, not multipart/form-data.

Try to make your request like that:

import requests


chat_id = '-351543550'
url = 'https://api.telegram.org/botXXXXXXXXXXXXXXX/sendPhoto?chat_id={}'.format(chat_id)
filepath = 'C:correctpathtoyourfile.jpg'
r = requests.post(url, files={"photo": open(filepath, 'rb')})  # note: files, not data
print(r.status_code)

P.S.: you can also send chat_id as form-encoded param by using

url = 'https://api.telegram.org/botXXXXXXXXXXXXXXX/sendPhoto'
...
r = requests.post(url, data={'chat_id': '-351543550'}, files={"photo": open(filepath, 'rb')})
Answered By: Ivan Vinogradov

In my case the solution proposed by Ivan Vinogradov did not work due to Cyrillic filenames. Changing the paths to Latin fixed it.

Answered By: Denis Gordeev

Using a Local Bot API Server you can send a large file up to 2GB.

GitHub Source Code:

https://github.com/tdlib/telegram-bot-api

Official Documentation

https://core.telegram.org/bots/api#using-a-local-bot-api-server

You can build and install this to your server by following the instructions on this link https://tdlib.github.io/telegram-bot-api/build.html

basic setup :

  1. Generate Telegram Applications id from https://my.telegram.org/apps
  2. Start the server ./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20
  3. Default address is http://127.0.0.1:8081/ and the port is 8081.
  4. All the official APIs will work with this setup. Just change the address to http://127.0.0.1:8081/bot<token>/METHOD_NAME reference: https://core.telegram.org/bots/api

Example Code with:

import requests

url = "http://127.0.0.1:8081/bot<your-bot-token>/sendVideo"

payload={'chat_id': 'chat_id',
'supports_streaming': 'true'}
files=[
  ('video',('your_file_name.mp4',open('path_of_file','rb'),'application/octet-stream'))
]
headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
Answered By: manukn