Python Requests Post Upload File
Python Requests is a popular library used for making HTTP requests in Python. It can handle various types of HTTP requests like GET, POST, PUT, DELETE, etc. In this post, we will see how to use Python Requests to do a file upload using the POST method.
Uploading a File using Python Requests
Uploading a file using Python Requests is very simple. We just need to create a dictionary that contains the file name and the file object and then pass it to the files
parameter of the requests.post()
method. Here's an example:
import requests
url = 'https://example.com/upload'
file_path = 'path/to/file'
file_name = 'example.txt'
with open(file_path, 'rb') as f:
files = {'file': (file_name, f)}
response = requests.post(url, files=files)
print(response.status_code)
In the above example, we first define the URL of the endpoint where we want to upload the file. We then specify the path of the file that we want to upload and also its name. We open the file using the open()
method in binary mode and create a dictionary that contains the file name and the file object. We then pass this dictionary to the files
parameter of the requests.post()
method.
The response object returned by the requests.post()
method contains information about the HTTP response like status code, headers, content, etc. In the example above, we are just printing the status code of the response.
Uploading Multiple Files
If you want to upload multiple files, you can simply create a list of dictionaries where each dictionary contains the file name and the file object. Here's an example:
import requests
url = 'https://example.com/upload'
file_paths = ['path/to/file1', 'path/to/file2']
file_names = ['example1.txt', 'example2.txt']
files = []
for file_path, file_name in zip(file_paths, file_names):
with open(file_path, 'rb') as f:
files.append(('files', (file_name, f)))
response = requests.post(url, files=files)
print(response.status_code)
In the above example, we define the URL of the endpoint where we want to upload the files. We then specify the paths and names of the files that we want to upload. We then create an empty list called files
. We then loop through the file paths and names and open each file using the open()
method in binary mode. We then append a tuple that contains the name of the field where the file will be uploaded and a dictionary that contains the file name and the file object to the files
list. Finally, we pass this list to the files
parameter of the requests.post()
method.
The response object returned by the requests.post()
method contains information about the HTTP response like status code, headers, content, etc. In the example above, we are just printing the status code of the response.
Conclusion
In this post, we saw how to use Python Requests to do a file upload using the POST method. We learned how to upload a single file and also how to upload multiple files. Python Requests is a powerful library that makes it easy to work with HTTP requests in Python. It is widely used in web scraping, data analysis, and other applications that require making HTTP requests.