user12983406
user12983406

Reputation: 21

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. Users can upload their files to my web server and I upload them to the file server. 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).

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)

Upvotes: 0

Views: 759

Answers (1)

mehrdadep
mehrdadep

Reputation: 1019

You are misusing the content-type header in your request. There is no need to manually set multipart/form and boundary if you're using files argument from requests. That is why you're getting unexpected end of file error. Try sending your request without that header.

r = requests.post(url, files=files)

Upvotes: 1

Related Questions