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.

SNMethodDescription
1GETIt is the most common method which can be used to send data in the unencrypted form to the server.
2HEADIt is similar to the GET but used without the response body.
3POSTIt is used to send the form data to the server. The server does not cache the data transmitted using the post method.
4PUTIt is used to replace all the current representation of the target resource with the uploaded content.
5DELETEIt is used to delete all the current representation of the target resource specified in the URL.

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.

POST 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.

login.html


  1. <html>  
  2.    <body>  
  3.       <form action = "http://localhost:5000/login" method = "post">  
  4.          <table>  
  5.         <tr><td>Name</td>  
  6.         <td><input type ="text" name ="uname"></td></tr>  
  7.         <tr><td>Password</td>  
  8.         <td><input type ="password" name ="pass"></td></tr>  
  9.         <tr><td><input type = "submit"></td></tr>  
  10.     </table>  
  11.       </form>  
  12.    </body>  
  13. </html>  

Now, Enter the following code into the script named post_example.py.

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=="ayush" and passwrd=="google":  

          return "Welcome %s" %uname  

   

if __name__ == '__main__':  

   app.run(debug = True)

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.

Flask HTTP methods

Give the required input and click Submit, we will get the following result.

Flask HTTP methods

Hence, the form data is sent to the development server by using the post method.

GET 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.

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.

get_example.py


  1. from flask import *  
  2. app = Flask(__name__)  
  3.   
  4.   
  5. @app.route('/login',methods = ['GET'])  
  6. def login():  
  7.       uname=request.args.get('uname')  
  8.       passwrd=request.args.get('pass')  
  9.       if uname=="ayush" and passwrd=="google":  
  10.           return "Welcome %s" %uname  
  11.    
  12. if __name__ == '__main__':  
  13.    app.run(debug = True)

Now, open the HTML file, login.html on the web browser and give the required input.

Flask HTTP methods

Now, click the submit button.

Flask HTTP methods

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.

uname = request.args.get('uname')  

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.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *