Flask Request Object

The flask object has a significant role to play because it is the request object that holds the data when the data is sent form an html form in the form of information until it reaches the server. The request object performs all the data processing, error handling, and other function too, while transferring the data to the server.

Or we can say that, In the client-server architecture, the request object contains all the data that is sent from the client-side to the server using HTTP methods like get, post. And all that data is temporarily stored in the request object till the server receives it.

SN Attribute Description
1 Methods By which data is sent from the client-side. i.e. get or post.
2 File It holds the info about the uploaded file.
3 Cookies These are the temporary short files that hold the names and values of cookies, which helps to track user session on the client-side.
4 Args Arguments are fetched from the URL address. It is part of the URL address that is declared in the URL address just after the '?'.
5 Form  It stores various key-value of form parameters.

Form data retrieval on the template

In the following example, the "URL" displays a web page ("customer1.html"), that contains a simple html form that is used to take the information as the input from the customer. The information filled by the user in the html form is sent to the http://localhost:5000/success using the POST method. The render_datafunction() function receives all information from the request object and displays it on the result_data.html page.

This application has three files, which are script2.py, customer.html, and result_data.html.

1. Script2.py

from flask import * 
 app = Flask(__name__) 
 @app.route('/') 
 def customer():
 return render_template('customer1.html')  
 @app.route('/success',methods = ['POST', 'GET']) 
 def render_datafunction(): 
 if request.method == 'POST': 
 result = request.form 
 return render_template("result_data.html",result = result) 
 if __name__ == '__main__': 
 app.run(debug = True)  
 </form> 
 </body> 
 </html> 

2. Customer1.html

<html> 
 <body> 
 <h3>Customer Registration</h3> 
 <p>Fill this form.</p>
 <form action = "http://localhost:5000/success" method = "POST">  
 <p>Name <input type = "text" name = "entername" /></p> 
 <p>Email <input type = "email" name = "enteremail" /></p> 
 <p>Contact <input type = "text" name = "entercontact" /></p> 
 <p>Pin code <input type ="number" name = "pin" /></p> 
 <p><input type = "submit" value = "submit" /></p>  

3. Result_data.html

<!doctype html> 
 <html> 
 <body> 
 <p><strong>Thanks for the registration. Confirm your details</strong></p> 
 <table border = 1>  
 {% for key, value in result.items() %} 
 <tr> 
 <th> {{ key }} </th> 
 <td> {{ value }} </td> 
 </tr>  
 {% endfor %} 
 </table> 
 </body> 
 </html>  

Firstly you must run the script.py file by using the command python script.py. This will start the development server on the localhost:5000, which can be later accessed on the browser, as given below:

Flask Request Object

Now click on the submit button, and output is shown in the given screenshot.

Flask Request Object2