Python

Python Requests Post Form Data

Uploading and downloading are very common when it comes to using the web and the internet. Whether it is simple to form data, audio, video, or any other type of data, we often need to download or upload things over the web. The high-level programming languages made it significantly easier to get or post stuff on the web by using their simple and useful libraries. Python programing language offers the “POST” function to send the form data over the web. In this guide, we’ll learn how to use the “POST” method to send form data on the web. So let us begin!

Python Requests Post Form Data

The POST method is provided by the python programming language to send the data to the server. The server could be anything, a general receiving node, a web interface, or anything else. Once the data is sent using the POST method, it is stored in the database for future and further use. The POST method is provided by the “requests” library in python. Now let us get to the fundamentals of the python requests post form data with the sample examples given below.

Example 1

We will begin with a simple and easy example so that you do not have any trouble understanding the working of python-requests post form data. The sample code given below will help you have an understanding of the basic functioning of the python requests post form data method. Let us first see the code below, and we will explain each line of code one by one:

import requests

url = 'https://httpbin.org/post'
form_data = {'user': 'value'}
server = requests.post(url, data=form_data)
output = server.text

print('The response from the server is: \n', output)

The first thing that you need to do is to import the requests library into the python program. This can be done using the “import requests” statement. Once you import the library, you can easily use its functions. Make sure you have pre-installed the requests library; if you get an error while including the requests library, you can simply install it with the “pip install requests” command. This will install the requests library and enable you to use the library in your programs.

After importing the requests library in the program, a URL is defined, which is going to be used to make the POST request. The data of the form to be posted is assigned to the form_data variable. The POST function is called with the requests.post() command. The defined URL and the data that needs to be posted are passed to the post() function. The server response is received by the server.text command. The response of the server is saved in the “output” variable, and using the print() command, it is displayed on the screen. Now let us see the output below:

As you can see, we provided the form_data = {“user” : “value”}, and it is displayed in the form of the server:

The rest of the variables show the respective values of the web server.

Example 2

Previously we explained a simple example for the python requests posts form data. Now let’s move to the more complicated example. Though, once you understand how the requests post method works in a python program, you can easily use it in your programs and handle complicated practical problems with it. In this example, we will try to explain how multi-row data can be sent to the server using the python requests post form data method. The reference code for your understanding is given in the screenshot below, have a look:

import requests

url = 'https://httpbin.org/post'
form_data = {

    'user': 'value',

    'Kalsoom': 'owner'}
server = requests.post(url, data=form_data)
output = server.text

print('The response from the server is: \n', output)

Here, we used the same code as we did in the last example; we only provided the multi-row data to the form_data variable. The form_data variable now holds the two rows of key and value pairs to be sent on the server using the requests post method. Let us see the output below to check how the posted data is represented on the server side. The output of the requests post method is as follows:

Now you can see that the data in the form_data variable is displayed under the “form” section on the server side.

This is the data that we have sent to the server in the request.post() command.

Example 3

Now let us learn how to incorporate the requests post method to post the data over the server, which is not form-encoded. This is a very common example of passing the string in the form_data instead of the dictionary of key: value pair. The simple string data can be sent directly with the requests post method. The code given below is for your reference:

import requests
import json

url = 'https://httpbin.org/post'
form_data = {
    'user': 'value',
    'Kalsoom': 'owner'}
server = requests.post(url, data=json.dumps(form_data))
output = server.text

print('The response from the server is: \n', output)

Here, you can note that we used the json.dumps() command to convert the dictionary key: value pair into a simple string. For using the json.dumps() method, we first imported the JSON library using the “import” JSON statement at the beginning of the program.

As we know that for using any function associated with a library, we need to explicitly include the library in the program. Hence, the JSON library is imported into the program to use the json.dumps() function. The dictionary key: value pair is passed to the json.dumps() function to convert it into a string. By using the print() command, the converted string, posted data, and its type is displayed. The output is as follows:

As you can see, the “data” field has the posted data, and the “form” field is empty. This is because the post data is not form-encoded, so it is stored in the “data” field instead of the “form” field. The posted data is stored in the “data” field in the string format instead of the dictionary key: value pair.

Conclusion

This is a complete guide on python requests post form data. With the help of examples, we learned how to efficiently write python codes, including the python requests post form data. The python requests post form data method is used by programmers to send the data to the server. The server can be a website, a simple node, or anything. The sample codes given in the examples will help you write customized codes for your practical applications. Practice these examples, and you will easily become familiar with the python requests post form data method.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content