我使用这个索取请求 https://api.telegram.org/file/bot<token>/<file_path>
,它允许我们从Telegram API下载文件。现在我需要在第二个POST请求(表单数据)中发送这个文件。
现在,我的代码引发了这样的错误。
Exception: embedded null byte Traceback
代码片段
# Download the file via GET request of the Telegram API.
response = requests.get("{0}/file/bot{1}/{2}".format(TELEGRAM_API_URL, telegram_bot_token, file_path))
files = [
('file', (file_name, open(response.content, 'rb'), 'application/octet-stream')) # error
]
# Upload the file via POST request.
response = requests.post(FILE_STORAGE_SERVICE_API_URL, files=files)
问题
如何正确转换文件,以便POST请求能够处理它?
发布于 2021-03-15 08:02:21
我从第一个请求中获取的response.content
的数据类型是<class 'bytes'>
。
工作代码片段:
response = requests.get("{0}/file/bot{1}/{2}".format(TELEGRAM_API_URL, telegram_bot_token, file_path))
files = {
"file": response.content
}
requests.post(FILE_STORAGE_SERVICE_API_URL, files=files)
https://stackoverflow.com/questions/66634221
复制相似问题