How do I use requests.put() to upload a file using Python?
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 = 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 :
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.
Related Query
- Use put_internal to upload file using python eve
- How to upload csv file to API using python
- How to use python loop to through file and execute queries using parameter from text file
- How to import other file using relative path to executed file not python command path?
- How to read the yaml file as dictionary and update value using python
- How can I convert complex .mat file to csv using python
- How to get HTTP error stream using Python requests library?
- How to search and play a mp3 file from a folder/directory using Python 3?
- How to append to the file using python 3
- How to delete file from SVN remote repository using python svn module
- how to upload quotation characters(«) to BigQuery tables using API in Python script
- How to download file using Python and authentication
- How to read text from a QlineEdit which belongs to a MainWindow class, and use it into a Qthread class using python and pyqt?
- How do I count the number of line in a FTP file without downloading it locally while using Python
- How to use the requests python module to login to fidelity.com
- How to build single .exe file with multiple files in python using cx_freeze
- How to extract a block of lines from given file using python
- How do I match an entire line from a text file and break it into variable using Python and RegEx?
- How to create PDF file in Python and put LaTeX data?
- How to split a file using shell commands in python
- how to access properties stored in a .properties file in a python file using ConfigParser
- How to send parameters in bitstamp api using python requests module
- How to save webpages text content as a text file using python
- How to set permissions on a file (key) in AWS S3 using Python Boto Library?
- how to remove blank lines from a csv file created using python
- How to download file using python when url doesn’t change
- How to Retrieve 10 first Google Search Results Using Python Requests
- How to put files in a zip archive using Python zipfile
- How to pick between two forms using python requests library?
- following webscraping in python tutorial they use urllib im using requests help translating instructions
- How to save a text file received in a POST request using python
- how to read text copied from web to txt file using python
- How to open a file using interactive click events in plot using python
- How to remove a line of a text file using python
- How can I use a string in a loop to be used in a dataframe command? Using Python 3
- How to Play a Mp3 file using Python in Windows?
- How to freeze Python PySide file using py2exe to one exe file?
- how to make the user choose the path of the output file using python
- how to upload to folder and replace files using python and Google Drive?
- Python how to skip commented lines from file using ArgumentParser.convert_arg_line_to_args
- Failure to use updated import file after alteration when using Jupyter for python development
- How to copy a whole file code into another file using python
- By using Paramiko module in Python how can I edit a file on Linux server?
- How do I put python images together using PIL?
- How can I use the built-in Python read() function in Julia using PyCall?
- How to copy CSV data to an existing xlsx file using Python
- How to import .mat -v7.3 file in python using h5py BUT with same ordering of dimension?
- How can use the ‘coding’ header of a python source file to read its contents properly?
- send an XML file to rest API using python requests
- How do create new column in csv file using python by shifting one row
More Query from same tag
- How do you add multiple unique Excel files in one long file? (Python)
- How do you make tables with previously stored strings?
- nested value from nested index python
- Filtering by values in text list in Python
- SQLAlchemy IndexError: list index out of range python in raspberry
- Equivalent ksdensity MATLAB function in Python
- Node editor, adding child QWidget another QWidget at run time
- Assign a constant and use it during the SQL queries
- How do I find an element by attribute value that contains certain text?
- Vowpal Wabbit: command not found
- Playsound only plays the mp3 file once and then gives the error «Permission Denied»
- Automate downloading images off Google
- What does pika’s on_open_error_callback method signature look like?
- TypeError : kollision() missing 1 required positional argument: ‘self’
- How to install an extension on Firefox while creating a webdriver through Robotframework
- python — register all subclasses
- can’t connect multiple HC-08 to raspberry pi using bluepy and Bluez
- Windows, Python: time.sleep() permission error
- Solving a symbolic matrix equation with SymPy
- MultiThreading with Python
- Merge function will only work for ordered list
- Pygame Flashing Sprite After Damage
- Datetime module in python
- Requests scraping mulitple elements from a list
- Can I add more than one behaviour in widgets in Kivy?
- Django Rest Framework serializers beautiful output
- Check postag patterns in python
- error UnboundLocalError: local variable ‘currentpl’ referenced before assignment:
- how to use logging adapter for sysout logs using bunyan in python
- How can I insert multiple rows with INSERT REPLACE in peewee?
- python3: different charset support
- Tkinter load screen with updating labels
- AWS Lambda Python package — no module named redis
- I want to move object back and forth in pygame but it only moves left and then it stop
- Pygame Collision on Rect
How to Upload Files with Python’s requests Library
Python is supported by many libraries which simplify data transfer over HTTP. The requests library is one of the most popular Python packages as it’s heavily used in web scraping. It’s also popular for interacting with servers! The library makes it easy to upload data in a popular format like JSON, but also makes it easy to upload files as well.
In this tutorial, we will take a look at how to upload files using Python’s requests library. The article will start by covering the requests library and the post() function signature. Next, we will cover how to upload a single file using the requests package. Last but not least, we upload multiple files in one request.
Uploading a Single File with Python’s Requests Library
This tutorial covers how to send the files, we’re not concerned about how they’re created. To follow along, create three files called my_file.txt , my_file_2.txt and my_file_3.txt .
The first thing we need to do is install our the request library in our workspace. While not necessary, it’s recommended that you install libraries in a virtual environment:
Activate the virtual environment so that we would no longer impact the global Python installation:
Now let’s install the requests library with pip :
Create a new file called single_uploader.py which will store our code. In that file, let’s begin by importing the requests library:
Now we’re set up to upload a file! When uploading a file, we need to open the file and stream the content. After all, we can’t upload a file we don’t have access to. We’ll do this with the open() function.
The open() function accepts two parameters: the path of the file and the mode. The path of the file can be an absolute path or a relative path to where the script is being run. If you’re uploading a file in the same directory, you can just use the file’s name.
The second argument, mode, will take the «read binary» value which is represented by rb . This argument tells the computer that we want to open the file in the read mode, and we wish to consume the data of the file in a binary format:
test_file = open("my_file.txt", "rb")
Note: it’s important to read the file in binary mode. The requests library typically determines the Content-Length header, which is a value in bytes. If the file is not read in bytes mode, the library may get an incorrect value for Content-Length , which would cause errors during file submission.
For this tutorial, we’ll make requests to the free httpbin service. This API allows developers to test their HTTP requests. Let’s create a variable that stores the URL we’ll post our files to:
test_url = "http://httpbin.org/post"
We now have everything to make the request. We’ll use the post() method of the requests library to upload the file. We need two arguments to make this work: the URL of the server and files property. We’ll also save the response in a variable, write the following code:
test_response = requests.post(test_url, files = "form_field_name": test_file>)
The files property takes a dictionary. The key is the name of the form field that accepts the file. The value is the bytes of the opened file you want to upload.
Normally to check if your post() method was successful we check the HTTP status code of the response. We can use the ok property of the response object, test_url . If it’s true, we’ll print out the response from the HTTP server, in this case, it will echo the request:
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
if test_response.ok: print("Upload completed successfully!") print(test_response.text) else: print("Something went wrong!")
Let’s try it out! In the terminal, execute your script with the python command:
Your output would be similar to this:
Upload completed successfully! < "args": <>, "data": "", "files": < "form_field_name": "This is my file\nI like my file\n" >, "form": <>, "headers": < "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "189", "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", "Host": "httpbin.org", "User-Agent": "python-requests/2.25.0", "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b" >, "json": null, "origin": "102.5.105.200", "url": "http://httpbin.org/post" >
As a sanity check, you can verify the form_field_name value matches what’s in your file.
Uploading Multiple Files with Python’s requests Library
Uploading multiple files using requests is quite similar to a single file, with the major difference being our use of lists. Create a new file called multi_uploader.py and the following setup code:
import requests test_url = "http://httpbin.org/post"
Now create a variable called test_files that’s a dictionary with multiple names and files:
test_files = < "test_file_1": open("my_file.txt", "rb"), "test_file_2": open("my_file_2.txt", "rb"), "test_file_3": open("my_file_3.txt", "rb") >
Like before, the keys are the names of the form fields and the values are the files in bytes.
We can also create our files variables as a list of tuples. Each tuple contains the name of the form field accepting the file, followed by the file’s contents in bytes:
test_files = [("test_file_1", open("my_file.txt", "rb")), ("test_file_2", open("my_file_2.txt", "rb")), ("test_file_3", open("my_file_3.txt", "rb"))]
Either works so choose whichever one you prefer!
Once the list of files is ready, you can send the request and check its response like before:
test_response = requests.post(test_url, files = test_files) if test_response.ok: print("Upload completed successfully!") print(test_response.text) else: print("Something went wrong!")
Execute this script with the python command:
Upload completed successfully! < "args": <>, "data": "", "files": < "test_file_1": "This is my file\nI like my file\n", "test_file_2": "All your base are belong to us\n", "test_file_3": "It's-a me, Mario!\n" >, "form": <>, "headers": < "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "470", "Content-Type": "multipart/form-data; boundary=4111c551fb8c61fd14af07bd5df5bb76", "Host": "httpbin.org", "User-Agent": "python-requests/2.25.0", "X-Amzn-Trace-Id": "Root=1-5fc3c744-30404a8b186cf91c7d239034" >, "json": null, "origin": "102.5.105.200", "url": "http://httpbin.org/post" >
Good job! You can upload single and multiple files with requests !
Conclusion
In this article, we learned how to upload files in Python using the requests library. Where it’s a single file or multiple files, only a few tweaks are needed with the post() method. We also verified our response to ensure that our uploads were successful.