[英]How to upload the file in POST requests (form-data)?
I use this GET request https://api.telegram.org/file/bot<token>/<file_path>
which allow us to download the file from Telegram API.我使用这个GET 请求
https://api.telegram.org/file/bot<token>/<file_path>
允许我们从 Telegram API 下载文件。 Now I need to send this file in the second POST request (form-data).现在我需要在第二个 POST 请求(表单数据)中发送这个文件。
Right now my code raises such error :现在我的代码引发了这样的错误:
Exception: embedded null byte Traceback
The code snippet :代码片段:
# 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)
Question :问题:
How do I convert a file correctly so that a POST request can process it?如何正确转换文件以便 POST 请求可以处理它?
The data type of the response.content
which I take from the first request is <class 'bytes'>
.我从第一个请求中获取的
response.content
的数据类型是<class 'bytes'>
。
The working code snippet:工作代码片段:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.