Skip to content Skip to sidebar Skip to footer

Python Requests Put Method Creates A Zero Byte File

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=('usernam

Solution 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

withopen(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)

Post a Comment for "Python Requests Put Method Creates A Zero Byte File"