0

I am trying to upload a file with PUT using the requests module in python.

my code is this:

with open(file, 'rb') as payload:
    r = requests.put(url, data=payload, auth=('username', 'password'))

The file is created, I am getting a response 200, but it has 0 bytes. If I am not doing something wrong I suspect that I have met the bug here.

Is this the case? If yes is there any workaround that I can try?

I've tried also the same with the httplib2 library

with open(file, 'rb') as payload:
    h = httplib2.Http(".cache")
    h.add_credentials('user', 'pass')
    resp, content = h.request(url, "PUT", body=payload)

But the request stays hanging for ever (again a 0 size file is created). Could it also be the same problem with the requests module?

[EDIT] Some extra info.

The service receiving the PUT is running on a ESXi hypervisor. It has a function where if you make an authed PUT request it stores the file of the request in /tmp. The server side is working (tested it with a perl script which does the same job and also with curl).

The file uploaded is a .tgz file which lies on my local filesystem and the url is in the form of "http://esx-server/tmp/file.tgz".

3
  • 2
    What service is receiving this PUT? Where is the file being created? Commented Jan 26, 2017 at 20:18
  • Try changing data to files and pass {'file': payload}. Commented Jan 26, 2017 at 21:56
  • @DavidCullenI have already tried it but still doesn't work Commented Jan 30, 2017 at 10:24

1 Answer 1

1

So it seems that there is some kind of bug indeed.

My solution was to use the urllib2. It isn't as "clean" as requests. But still better than what I thought it would be.

My working code now is:

import urllib2
from base64 import b64encode

with open(source, 'rb') as file:
    data = file.read()
request = urllib2.Request(url)
request.add_data(data)
request.add_header('Authorization', 'Basic ' + b64encode(username + ':' + password))
request.get_method = lambda: "PUT"
r = urllib2.urlopen(request)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.