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?

sdgd
  • 723
  • 1
  • 17
  • 38
  • 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`? – cody Dec 11 '18 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 – sdgd Dec 11 '18 at 15:32
  • But just to be clear, the `curl` command you listed works fine? – cody Dec 11 '18 at 15:33
  • yes, it works perfectly fine. it's uploading the files. – sdgd Dec 11 '18 at 15:35

2 Answers2

6

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

S.Roman
  • 61
  • 2
1

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

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

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • 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? – sdgd Dec 11 '18 at 15:32