Flask HTTP methods
HTTP is the hypertext transfer protocol which is considered as the foundation of the data transfer in the world wide web. All web frameworks including flask need to provide several HTTP methods for data communication.
The methods are given in the following table.
We can specify which HTTP method to be used to handle the requests in the route() function of the Flask class. By default, the requests are handled by the GET() method.
To handle the POST requests at the server, let us first create a form to get some data at the client side from the user, and we will try to access this data on the server by using the POST request.
Now, Enter the following code into the script named post_example.py.
Now, start the development server by running the script using python post_exmple.py and open login.html on the web browser as shown in the following image.
Give the required input and click Submit, we will get the following result.
Hence, the form data is sent to the development server by using the post method.
Let's consider the same example for the Get method. However, there are some changes in the data retrieval syntax on the server side. First, create a form as login.html.
Now, create the following python script as get_example.py.
Now, open the HTML file, login.html on the web browser and give the required input.
Now, click the submit button.
As we can check the result. The data sent using the get() method is retrieved on the development server.
The data is obtained by using the following line of code.
Here, the args is a dictionary object which contains the list of pairs of form parameter and its corresponding value.
In the above image, we can also check the URL which also contains the data sent with the request to the server. This is an important difference between the GET requests and the POST requests as the data sent to the server is not shown in the URL on the browser in the POST requests.