sdream sdream - 2 months ago 14
Python Question

post request with Python requests lib returning status 405

I am uploading a file to server using requests lib in Python. I read its documentation and some stackoverflow questions and implemented following code:

url = ""
id = "user-id"
password = "password"
headers = {'content-type': 'application/x-www-form-urlencoded'}

with open(file_path, 'rb') as f:
response =, files={'file':f}, auth=HTTPBasicAuth(username=id, password=password),headers=headers)

But this code is not working, response.status_code returning 405 and response.reason returning
Method Not Allowed
. When i upload file using curl command on terminal it works fine and file gets uploaded:

curl -u user-id:password -T file/path/on/local/machine/file.csv ""

Can someone please help here.

Answer Source

Related question here. In reality, curl --upload-file performs a PUT not a POST. If you want to mimic what curl does, you might want to try:

with open(file_path, 'rb') as f:
    response = requests.put(url=url, files={'file':f}, auth=HTTPBasicAuth(username=id, password=password), headers=headers)