[英]Uploading and uploaded file without saving with python requests
I have a web server that acts as a proxy between users and a file server.我有一个 web 服务器,它充当用户和文件服务器之间的代理。 Users can upload their files to my web server and I upload them to the file server.
用户可以将他们的文件上传到我的 web 服务器,我将它们上传到文件服务器。 I want to be able to do this without saving the temp uploaded file but every time I get unexpected end of file error from the file server.
我希望能够在不保存临时上传文件的情况下执行此操作,但每次我从文件服务器收到意外的文件结束错误。 This is my code (I'm using django rest framework for my APIs).
这是我的代码(我的 API 使用 django rest 框架)。
headers = {"content-type":"multipart/form; boundary={}".format(uuid.uuid4().hex)}
files = []
for f in request.FILES.getlist('file'):
files.append((f.name, open(f.file.name,'rb'), f.content_type))
files_dict = {'file': files}
r = requests.post(url, files=files, headers=headers)
You are misusing the content-type
header in your request.您在请求中滥用了
content-type
header。 There is no need to manually set multipart/form
and boundary
if you're using files
argument from requests
.如果您使用
requests
中的files
参数,则无需手动设置multipart/form
和boundary
。 That is why you're getting unexpected end of file error.这就是为什么您会收到意外的文件结束错误。 Try sending your request without that header.
尝试在没有 header 的情况下发送您的请求。
r = requests.post(url, files=files)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.