3

I'm having a zip file that needs to be uploaded. When I use the CURL command, it is uploading it but when I try the same using Python Requests, i get HTTP 405 Method Not Allowed. The zip file is usually around 500kb.

Curl command -

curl -u<username>:<password> -T /log/system/check/index.zip "<target URL>"

Python Script (tried 2 different ways) -

1:

import requests
files = {'file': open('/log/system/check/index.zip', 'rb')}
r = requests.post(url, files=files, auth=('<username>', '<password>'))

2:

import requests
fileobj = open('/log/system/check/index.zip', 'rb')
r = requests.post(url, auth=('<username>', '<password>'), files={"archive": ("index.zip", fileobj)})

Am I missing something obvious?

4
  • 1
    So the curl command succeeds? The curl manpage says this of -T: If this is used on an HTTP(S) server, the PUT command will be used. So, curl is using PUT, not POST. Does your endpoint allow POST? Commented Dec 11, 2018 at 14:48
  • i tried using PUT now and see the error - Tunnel connection failed: 503 Service Unavailable', not sure if any proxy is required Commented Dec 11, 2018 at 15:32
  • But just to be clear, the curl command you listed works fine? Commented Dec 11, 2018 at 15:33
  • yes, it works perfectly fine. it's uploading the files. Commented Dec 11, 2018 at 15:35

2 Answers 2

5

may be this will help you.

 with open(zipname, 'rb') as f:
uploadbase = requests.put('url',
                    auth=(base, pwd),
                    data=f,
                    headers={'X-File-Name' : zipname, 
                                  'Content-Disposition': 'form-data; name="{0}"; filename="{0}"'.format(zipname), 
                                   'content-type': 'multipart/form-data'})

the difference between put and post

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks save my day
1

curl -T ... uses PUT method instead of POST. As the error message says, you should use

r = requests.put(url, ...)

1 Comment

i tried using PUT now and see the error - Tunnel connection failed: 503 Service Unavailable', not sure if any proxy is required, am i missing anything?

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.