Table of contents
1.
Introduction
2.
HTTP Methods in Flask
3.
POST Method
3.1.
Implementation
3.1.1.
login.html
3.1.2.
post_example.py
4.
GET Method
4.1.
Implementation
4.1.1.
login.html
4.1.2.
get_example.py
5.
Frequently Asked Questions (FAQs)
6.
Key Takeaways
Last Updated: Mar 27, 2024

HTTP Methods in Flask

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Source: Link

Introduction

Flask is a web framework; it is a Python module that lets us develop web applications easily. It has a small and easy-to-extend core and it's a microframework that doesn't include an ORM (Object Relational Manager) or such features.

The Hypertext Transfer Protocol (HTTP) was established to allow clients and servers to communicate with each other.

Between a client and a server, HTTP serves as a request-response protocol.

Consider a client that wants to send an HTTP request to the server, which will be followed by a response from the server. The client's response will include information about the request's status as well as the content that was requested.

The GET and POST methods are the most popular HTTP methods/requests.

HTTP Methods in Flask

HTTP stands for hypertext transfer protocol, and it is the foundation of data transfer on the internet. All web frameworks, including Flask, must have many HTTP methods for data transfer.

The methods are shown below.

HTTP Methods

Purpose

GET

It is the most frequent technique for sending data to the server in an unencrypted format.

HEAD 

It's similar to GET, but it doesn't include the response body.

POST

It's utilised to deliver the data from the form to the server. The data sent via the post method is not cached by the server.

DELETE

It is used to remove all current representations of the URL-specified target resource.

PUT

It replaces all of the target resource's current representations with the uploaded material.

 

The route() function of the Flask class allows us to define which HTTP method should be used to process requests. The GET() function is used to handle requests by default.

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!

 

Live masterclass