How to Upload File with Python Requests

How to upload file with python requests?

If upload_file is meant to be the file, use:

files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}

r = requests.post(url, files=files, data=values)

and requests will send a multi-part form POST body with the upload_file field set to the contents of the file.txt file.

The filename will be included in the mime header for the specific field:

>>> import requests
>>> open('file.txt', 'wb') # create an empty demo file
<_io.BufferedWriter name='file.txt'>
>>> files = {'upload_file': open('file.txt', 'rb')}
>>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii'))
--c226ce13d09842658ffbd31e0563c6bd
Content-Disposition: form-data; name="upload_file"; filename="file.txt"

--c226ce13d09842658ffbd31e0563c6bd--

Note the filename="file.txt" parameter.

You can use a tuple for the files mapping value, with between 2 and 4 elements, if you need more control. The first element is the filename, followed by the contents, and an optional content-type header value and an optional mapping of additional headers:

files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}

This sets an alternative filename and content type, leaving out the optional headers.

If you are meaning the whole POST body to be taken from a file (with no other fields specified), then don't use the files parameter, just post the file directly as data. You then may want to set a Content-Type header too, as none will be set otherwise. See Python requests - POST data from a file.

python requests upload file

A few points :

  • make sure to submit your request to the correct url ( the form 'action' )
  • use the data parameter to submit other form fields ( 'dir', 'submit' )
  • include the name of the file in files ( this is optional )

code :

import requests

url = 'http://example.com' + '/upload.php'
data = {'dir':'/uploads/', 'submit':'Submit'}
files = {'file':('1.jpg', open('1.jpg', 'rb'))}
r = requests.post(url, data=data, files=files)

print(r.content)

How do I use requests.put() to upload a file using Python?

Updated

Using requests.put() with the files parameter sends a multipart/form-data encoded request which the server does not seem to be able to handle without corrupting the data, even when the correct content type is declared.

The curl command simply performs a PUT with the raw data contained in the body of the request. You can create a similar request by passing the file data in the data parameter. Specify the content type in the header:

headers = {'Content-type': 'image/jpeg', 'Slug': fileName}
r = requests.put(url, data=open(path, 'rb'), headers=headers, auth=('username', 'pass'))

You can vary the Content-type header to suit the payload as required.


Try setting the Content-type for the file.

If you are sure that it is a text file then try text/plain which you used in your curl command - even though you would appear to be uploading a jpeg file? However, for a jpeg image, you should use image/jpeg.

Otherwise for arbitrary binary data you can use application/octet-stream:

openBin = {'file': (fileName, open(path,'rb'), 'image/jpeg' )}

Also it is not necessary to explicitly read the file contents in your code, requests will do that for you, so just pass the open file handle as shown above.

File upload using python requests post

In the headers we should not pass 'Content-Type': 'application/gzip' while doing post . After removing that it worked

Working code

>>> headers
{'referer': 'https://dummy.test.com', 'cookie': 'i18next=; session=17158f23bdd99dd1_62179b51.a2uP2cSYO_1ASd4fpfKuQaa2jtU; csrftoken=##1440ee62328990eadd4bb9d1bdf1f15bd6a973cb; hpeuck_cktst=', 'x-csrf-token': '##1440ee62328990eadd4bb9d1bdf1f15bd6a973cb', 'X-Requested-With': 'XMLHttpRequest'}
>>> files
{'file': ('cop-pem-cert.tar.gz', <_io.BufferedReader name='cop-pem-cert.tar.gz'>, 'application/gzip')}
>>> url
'https://dummy.test.com/data-migration/upload'
>>> a=requests.post(url,headers=headers,files=files)
>>> a=requests.post(url,headers=headers,files=files)
>>> a
<Response [200]>

Uploading a file via requests.put in python

According to the documentation here, data can accept "dictionary, list of tuples, bytes, or file-like object."

This should work:

with open(mp4_file_path, 'rb') as finput:
response = requests.put('https://my-app-domain.com', data=finput.read())


Related Topics



Leave a reply



Submit