Python programming language is a high-level language supported by many libraries. The libraries of python make it simple to upload data over the server. In this article, we will explore the requests library to use the post request to send files to the web. The requests library on python is commonly used by python developers for server interactions and web scraping. You cannot just upload the JSON data over the web but also post the files using the requests post command. Here, we will guide you on how to upload a file using the python requests post file method.
What is the Python Requests Post File Method?
The request library of python is the most popular library used for web scraping. It offers a get and post method to get or post the data from or to the server. The requests post method is being used by programmers to send the data over to the server. The data could be a simple string, a key: value pair of the dictionary, a JSON type data, a file, or anything else. The python requests post file method is specifically used to post the files over the network or server. Now let us learn how to send the file over the network using the python requests post method with the help of examples.
Example 1
To help you comprehend how to use the POST method to upload files to the server, let’s start with a straightforward example. Uploading a file on the server using the requests post method is very easy. Once you understand the process, you can easily upload a bulk of files on the server without having to get help from anyone. In this example, we will help you learn how to post a file on the web with the python requests post method by specifying the file name. The sample code is given below for your reference, have a look at it first, and then we will explain each line one by one:
myurl = 'https://httpbin.org/post'
f = {'file data': open('TestFile.txt', 'rb')}
res = requests.post(myurl, files=f)
print(res.text)
To use the post function, we first loaded the requests library into the program. To use the function of any library, the library itself should be included in the program explicitly. So, the “import requests” statement is used to import the requests library in the program. After that, the URL is specified. This is the url upon which the post request is to be made. The file “TestFile” is to be opened in the “read” and “binary” modes by using the “open” function. The “rb” in the “open()” function represents the “read” and “binary” format of the file that is to be opened. The key: value pair of the dictionary is provided in the files parameter of the post() function.
The “URL” and the “file” parameters are passed to the requests, post() function to make the post request to the URL and send the specified to the URL. The response to the requests.post() command is stored in the “res” variable, and using the print() command, it is shown on the screen. Now let us see the output given in the screenshot below to have a better understanding of the working of the python requests post method. Here is the output:
Example 2
In the previous example, we made the post request to open the file in the read mode. When a file is open in the read mode, you cannot make any changes to it; you can only read whatever is already present in the file. We just uploaded a file with the post method and instructed it to open the file in the “read” mode so that we can see what the file contains. Now, in this example, we will show that using the requests post method, we can send the data to be entered in the file. Yes, you read it correctly; you can send data of the file using the requests post method. See the code given in the screenshot below to understand how to do it. Here is the sample code:
myurl = 'https://httpbin.org/post'
f = {'file data': open('TestFile.txt', 'New data')}
res = requests.post(myurl, files=f)
print(res.text)
Previously, we made the post request to open the file in read and binary mode by specifying the “rb” parameter with the file name. Here you can see that instead of providing the mode of the file, we provide the data to be posted on the file. All lines of the code are the same as we used in the previous example; just the data to be sent by the post method is changed. Now let us verify whether the data has been uploaded to the file or not by checking the output given below:
Example 3
So far, we have learned how to post a file or post the data to the file using the requests post method. Now let us learn how to upload multiple files using the requests post method. Yes, you can upload many files simultaneously using the requests post method. It is as simple as posting a single file at a time. For that, you simply need to provide the name and some important parameters of the file. Let us see the sample code given in the screenshot below to learn how to upload multiple files together using the python requests post file method:
url = "http://httpbin.org/post"
form_data = {
"file1": open("TestFile1.txt", "rb"),
"file2": open("TestFile2.txt", "rb")
}
res = requests.post(url, files = f)
if res.ok:
print("Upload complete!")
print(res.text)
else:
print("Error Alert!")
Here, the requests library is imported, then the URL is defined to which the post request is to be made. And after that, the name of the file and their respective parameters are provided, separated by a “,” comma. You can upload as many files as you want; data for each file can be provided the same way as it has been done for the two files in the program above. A key: value pair will be used for each file to be uploaded, each pair separated by a “,” comma. The following response you will get from the requests post-call, in case of successful file upload:
Conclusion
We quickly went over the python requests post file methods to upload the files on the server. The requests library of python is the most popular power library to be used for web scraping. The post method of the requests library is used to post some data or files over the network or to the server. We studied how the Python requests post method functions with the aid of examples.