sdgd
sdgd

Reputation: 733

Python Requests - Uploading a Zip file

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?

Upvotes: 3

Views: 8175

Answers (2)

S.Roman
S.Roman

Reputation: 61

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

Upvotes: 6

Maurice Meyer
Maurice Meyer

Reputation: 18106

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

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

Upvotes: 1

Related Questions