Flask HTTP Methods

Flask HTTP Methods

HTTP stands for Hypertext Transfer Protocol that is used to transfer data all over the world wide web(WWW), or we can say how messages are divided (formatted) and transmitted and what actions servers and Web browsers must perform in response to several commands.

For example

 When you enter a URL in the browser, it sends an HTTP command to the web server that directs you to the search and redirects to the requested web page. The other primary standard that controls the operation of the World Wide Web is HTML, which covers how Web pages are designed and displayed.

There are mainly five types of HTTP methods.

  • Post
  • Get
  • Put
  • Delete
  • Head
SN Method Definition
1 POST It is used to send the form data to the server. The data transferred is stored in the HTTP request body.
2 GET It is the most common way of sending data to the server in the unencrypted form and attached to the URL.
3 PUT It is used to replace all the current target resource representation with the content uploaded and produce the same result.
4 DELETE It is used to remove all the current target resource representation specified in the URL
5 HEAD It is similar to the GET but it returns list of users without response body.

Post methods 

The post method is used to send data securely from an html form to the server. The POST method does not remain in browser history and also don’t have any kind of boundation or world limit. And it passes data in the body of the HTTP request.

We can understand the post method with the help of an example in which we make an Html form and ask the user to input information in that html form, and we are going to access that information on the localhost server using the post methods.

Remark:-Post methods are more secure than the get method because in get method, the data is transported in the unencrypted form, and anyone can easily see data in the URL.

Now open the text editor and copy the given code and save it with .html extension in the template folder that we created at the starting of the tutorial.

 <html>  
 <body>  
 <form action = "http://localhost:5000/login" method = "post">  
 <table>  
 <tr><td>Email</td>   
 <td><input type ="email" name ="mail"></td></tr>  
 <tr><td>Password</td>  
 <td><input type ="password" name ="key"></td></tr>  
 <tr><td><input type = "submit"></td></tr>  
 </table>  
 </form>   
 </body>  
 </html> 

Now open the text editor and write the following code and save it with (.py) extension outside the template folder.

from flask import *  
 app = Flask(__name__)  
 @app.route('/login',methods = ['POST'])  
 def login():  
 mail=request.form['mail']   
 passwrd=request.form['key']  
 if mail=="[email protected]" and key=="akash":  
 return "Hi, %s"mail   
 if __name__ == '__main__':  
 app.run(debug = True) 

Now run the server and execute the above code as shown in the below screenshot using the following commands

  • Py –m  veneenv
  • App\scripts\activate
  • Set FLASK_APP=post_example.py
  • Flask  run
Flask HTTP Methods

Now copy the address ( http://1270.0.:5000/) and paste it into the web browsers and then press enter, now we can see the output in the form of the web page, as shown in the given screenshot.

Flask HTTP Methods2

After entering the values, click on the submit button.

Flask HTTP Methods3

Get method

As we discussed above that the Get method is less secure than the post method because, in the get method, the data is transferred in the unencrypted form and can be seen in the URL by anyone.

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, create the following python script as 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=="akash" and passwrd=="google":  
 return "Welcome %s" %uname  
 if __name__ == '__main__':  
 app.run(debug = True) 

Now again, run the server and execute the above code, as shown in the below screenshot using the following commands.

  • Py –m  veneenv
  • App\scripts\activate
  • Set FLASK_APP=post_example.py
  • Flask  run
Flask HTTP Methods4

Now copy the highlighted URL address and paste it in the web browser and then press enter buttons.

Flask HTTP Methods5

then, click on the submit button.

Flask HTTP Methods6