Saurabh Jain
Saurabh Jain

Reputation: 1287

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 = "http://example.com/file.csv"
id = "user-id"
password = "password"
headers = {'content-type': 'application/x-www-form-urlencoded'}

with open(file_path, 'rb') as f:
    response = requests.post(url=url, 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 "http://example.com/file.csv"

Can someone please help here.

Upvotes: 2

Views: 2064

Answers (1)

Trein
Trein

Reputation: 3688

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)

Upvotes: 2

Related Questions