向 Telegram 发送图像时出现 'Request Entity Too Large' 错误的原因是什么?
What makes the 'Request Entity Too Large' error when sending an image to Telegram?
文件大小:51.2 KB
正在尝试发送:
>>> 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''
>>>
我也尝试了一些其他请求,例如:
photo = open(('C:/Users/Administrator/Downloads/WhatsAppImage.jpeg').encode('utf-8'), 'rb')
r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': photo})
和:
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})
最后一个选项给我下一个错误:
>>> r
<Response [400]>
>>> r.reason
'Bad Request'
你可能做错了。
正如 Bot API 文档所说:
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,通过使用 data=
关键字参数,您使用 form-encoded
类型发送有效负载,而不是 multipart/form-data
.
试着这样提出你的要求:
import requests
chat_id = '-351543550'
url = 'https://api.telegram.org/botXXXXXXXXXXXXXXX/sendPhoto?chat_id={}'.format(chat_id)
filepath = 'C:\correct\path\to\your\file.jpg'
r = requests.post(url, files={"photo": open(filepath, 'rb')}) # note: files, not data
print(r.status_code)
P.S.: 您也可以使用
将 chat_id
作为 form-encoded
参数发送
url = 'https://api.telegram.org/botXXXXXXXXXXXXXXX/sendPhoto'
...
r = requests.post(url, data={'chat_id': '-351543550'}, files={"photo": open(filepath, 'rb')})
在我的例子中,由于西里尔文文件名,Ivan Vinogradov 提出的解决方案不起作用。将路径更改为拉丁语修复了它。
文件大小:51.2 KB 正在尝试发送:
>>> 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''
>>>
我也尝试了一些其他请求,例如:
photo = open(('C:/Users/Administrator/Downloads/WhatsAppImage.jpeg').encode('utf-8'), 'rb')
r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': photo})
和:
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})
最后一个选项给我下一个错误:
>>> r
<Response [400]>
>>> r.reason
'Bad Request'
你可能做错了。
正如 Bot API 文档所说:
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,通过使用 data=
关键字参数,您使用 form-encoded
类型发送有效负载,而不是 multipart/form-data
.
试着这样提出你的要求:
import requests
chat_id = '-351543550'
url = 'https://api.telegram.org/botXXXXXXXXXXXXXXX/sendPhoto?chat_id={}'.format(chat_id)
filepath = 'C:\correct\path\to\your\file.jpg'
r = requests.post(url, files={"photo": open(filepath, 'rb')}) # note: files, not data
print(r.status_code)
P.S.: 您也可以使用
将chat_id
作为 form-encoded
参数发送
url = 'https://api.telegram.org/botXXXXXXXXXXXXXXX/sendPhoto'
...
r = requests.post(url, data={'chat_id': '-351543550'}, files={"photo": open(filepath, 'rb')})
在我的例子中,由于西里尔文文件名,Ivan Vinogradov 提出的解决方案不起作用。将路径更改为拉丁语修复了它。