Python requests file POST error (encoding??)

Asked 1 years ago, Updated 1 years ago, 80 views

You are about to send a POST request to https://api.telegram.org/bot~~~/sendaudio to send a file to the Telegram bot.

URL = 'https://api.telegram.org/bot<...>/sendaudio'
VALUE = {'chat_id': '<...>'}
FILE = {'audio': open('a.mp3', 'rb')}

r = requests.post(URL, data=VALUE, files=FILE)

If you do this, the file will be transferred normally and the server will respond.

By the way, the file name is

FILE = {'audio': open('あ.mp3', 'rb')} or FILE = {'audio': open('ga.mp3', 'rb')}

If you do this and do the same, the server responds that it has not received the audio. It seems to be an error that occurs in the process of using requests because it works normally if it is sent through Postman, so how can I solve it? ㅠ 다른 Or is there any other way to implement the same functionality?

python requests

2022-09-22 18:02

2 Answers

Please use the open function of the codecs module below.

import codecs
codecs.open(u'hangeul file name).mp3', 'rb')


2022-09-22 18:02

Using Python 3.5.3 version...

>>> FILE = {u'audio': codecs.open(u'hi).mp3', 'rb')}
>>> r = requests.post(URL, data=VALUE, files=FILE)
>>> r
<Response [413]>
>>> r = requests.post(URL, data=VALUE, files=FILE)
>>> r
<Response [400]>
>>> r = requests.post(URL, data=VALUE, files=FILE)
>>> r
<Response [400]>
>>> r
<Response [400]>
>>> FILE = {u'audio': codecs.open(u'hi.mp3', 'rb')}
>>> r = requests.post(URL, data=VALUE, files=FILE)
>>> r
<Response [413]>
>>> r = requests.post(URL, data=VALUE, files=FILE)
>>> r
<Response [400]>
>>> r = requests.post(URL, data=VALUE, files=FILE)
>>> r
<Response [400]>

After checking it again and trying several times, the first request sent after creating a file object in this way returns 413 responses, but the rest returns to 400. 텔레ㅠ I think there is a problem with the Telegram server side... I don't know... (Crying)


2022-09-22 18:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.