POST Method
The POST request is one of HTTP's supported requests. The POST request indicates that the data in the body of the message is also accepted by the web server. The WWW (World Wide Web) regularly uses POST to transmit user-generated data to the web server.
Implementation
To handle POST requests on the server, we will first develop a form to collect data from the user on the client-side, and then utilise the POST request to access this data on the server.
login.html
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<table>
<tr><td>Name</td>
<td><input type ="text" name ="uname"></td></tr>
<tr><td>Password</td>
<td><input type ="password" name ="pass"></td></tr>
<tr><td><input type = "submit"></td></tr>
</table>
</form>
</body>
</html>
Now, in the post_example.py script, we will write the following code.
post_example.py
from flask import *
app = Flask(__name__)
@app.route('/login',methods = ['POST'])
def login():
uname=request.form['uname']
passwrd=request.form['pass']
if uname=="pranay" and passwrd=="codingninjas":
return "Welcome %s" %uname
if __name__ == '__main__':
app.run(debug = True)

You can also try this code with Online Python Compiler
Run Code
We will start the development server by running the python ‘post_example.py’ script and on opening ‘login.html’ in our web browser, we will get the following output.

We'll obtain the following result if we enter the relevant information and click Submit.

As a result, the post method is used to send the form data to the development server.
GET Method
The GET request is commonly used to obtain information from a server. It's also used to attach form data to URLs(Uniform Resource Locators) in the name-value pair format. If we utilise GET, the URL length will be limited. It also aids in the submission of the result's bookmark. When the data does not require any form of protection or contain any graphics or files, GET is preferable.
Implementation
Consider the identical scenario with the Get technique. On the server side, however, there are some changes to the data retrieval syntax. We will create a ‘login.html’ form first.
login.html
<html>
<body>
<form action = "http://localhost:5000/login" method = "get">
<table>
<tr><td>Name</td>
<td><input type ="text" name ="uname"></td></tr>
<tr><td>Password</td>
<td><input type ="password" name ="pass"></td></tr>
<tr><td><input type = "submit"></td></tr>
</table>
</form>
</body>
</html>
Now, in the ‘get_example.py’ script, we will write the following code.
get_example.py
from flask import *
app = Flask(__name__)
@app.route('/login',methods = ['GET'])
def login():
uname=request.args.get('uname')
passwrd=request.args.get('pass')
if uname=="pranay" and passwrd=="codingninjas":
return "Welcome %s" %uname
if __name__ == '__main__':
app.run(debug = True)

You can also try this code with Online Python Compiler
Run Code
Now, open the HTML file, login.html on the web browser and give the required input.

Now, click the submit button.

As we can see from the outcome. On the development server, the data submitted via the get() method is obtained.
The following line of code is used to obtain the data.
request.args.get('uname') = uname
The args object is a dictionary object that holds a list of form parameter pairs and their values.
We can also see the URL in the preceding screenshot, which includes the data delivered with the request to the server. The data supplied to the server in POST requests is not displayed in the URL on the browser, which is a significant distinction between GET and POST queries.
Click here to know about, PHP For Loop
Frequently Asked Questions (FAQs)
1. What is Flask?
Flask is a web framework; it's a Python module that lets you develop web applications easily. It's has a small and easy-to-extend core: it's a microframework that doesn't include an ORM (Object Relational Manager) or such features.
2. What is an HTTP request?
The Hypertext Transfer Protocol (HTTP) was established to allow clients and servers to communicate.
3. What is a GET request?
The GET request is commonly used to obtain information from a server. It's also used to attach form data to URLs in the name-value pair format.
4. What is a POST request?
The POST request is one of HTTP's supported requests. The POST request indicates that the data in the body of the message is also accepted by the web server.
5. What is a PUT request?
The PUT request replaces all of the target resource's current representations with the uploaded material.
Key Takeaways
We learned about the concept of HTTP methods in Flask. We learned about two important HTTP requests, GET and POST implementation. We learnt about the key implementation differences between the GET and POST methods in Flask.
For more information about Flask, You can also expand your knowledge by referring to these articles on Flask.
Happy Learning!