简体   繁体   English

通过 post 请求将二进制数据作为文件提交

[英]Submitting binary data as a file via post request

I know how to submit a post request with a file.我知道如何使用文件提交发布请求。

files = {'file': open('local.pdf', 'rb')}
r = requests.post(url, files=files)

Since I'm downloading the file from a response, I want to avoid writing the response.contents to my local disk ('local.pdf') before I submit the post request.由于我是从响应中下载文件,因此在提交发布请求之前,我想避免将response.contents写入本地磁盘('local.pdf')。 Can I submit the file as a bytes object?我可以将文件作为字节 object 提交吗?

You can use io.BytesIO to do that.您可以使用io.BytesIO来做到这一点。

Here is an example:这是一个例子:

rawData = io.BytesIO(b"Some data: \x00\x01") # Change the content
files = {'file': rawData}
r = requests.post(url, files=files)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM