Python Flask-Get and Post method

Get and Post methods in the flask:

Using Python programming language, we will see the HTTP Get and Post method in the flask.
Http protocol is the foundation of data communication on the world wide web. Different methods of data retrieval from a specified URL are defined in this protocol.

The following table to recall the different HTTP methods −
Methods & Description:

  • GET- Sends data in unencrypted form to the server. Most common method.
  • HEAD- Same as getting, but without the response body
  • POST- It sends HTML form data to the server. Data received by the POST method is not cached by the server.
  • PUT- Replaces all current representations of the target resource with the uploaded content.
  • DELETE- It removes all current representations of the target resource given by a URL
The route of the flask will respond to the GET requests by default. By providing methods argument to route() decorator this preferences can be altered.
To determine the use of the POST method in URL Routing, let us create an HTML form and use the POST method to send form data to a URL. 
login.html

<html>
   <body>
      
      <form action = "http://localhost:5000/login" method = "post">
         <p>Enter Name:</p>
         <p><input type = "text" name = "vm" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>

   </body>
</html>      
Now enter the following script.
from flask import Flask, redirect, url_for, request
app = Flask(__name__)

@app.route ('/success/')
def success(name):
   return 'welcome %s' % name

@app.route('/login’, methods = ['POST', 'GET'])
def login():
   if request. method == 'POST':
      user = request. form['vm']
      return redirect(url_for('success’, name = user))
   else:
      user = request.args.get('vm')
      return redirect(url_for('success’, name = user))

if __name__ == '__main__':
   app. run(debug = True)
After the development server starts running, open /login.html in the browser, enter the name in the text field and click Submit.
Form data is posted to the URL in the action clause of the form tag.
http://localhost/login is mapped to the login() function. Since the server has received data by POST method, a value of ‘nm’ parameter obtained from the form data is obtained by –
user = request. form['VM']
It is passed to /success URL as a variable part. The browser displays a welcome message in the window.
The method parameter is to change into ‘GET’ in login.html and open it with the browser. The data received on the server is by the GET method. 
The value of ‘VM’ parameter is now obtained by 
User = request.args.get (‘vm’)
A list of pairs of form parameters and their corresponding value in a dictionary object is called args. The value corresponding to the ‘vm’ parameter is passed on to the ‘/success’ URL as before.