Python Flask - Request Object

Last Updated : 26 Jun, 2026

The request object in Flask contains all the data associated with an incoming HTTP request. It allows applications to access information sent by the client, such as form data, query parameters, uploaded files, cookies, headers and the HTTP request method. It's provided by Flask and represents the current HTTP request sent by the client to the server. It's used for:

  • Retrieve form data submitted by users.
  • Access URL query parameters.
  • Read uploaded files.
  • Access cookies and request headers.
  • Determine the HTTP request method (GET, POST, etc.).

Request Object Attributes

AttributeDescription
request.formRetrieves data submitted through HTML forms using the POST method.
request.argsRetrieves query parameters from the URL.
request.filesAccesses uploaded files.
request.cookiesRetrieves cookies sent by the browser.
request.methodReturns the HTTP request method (GET, POST, etc.).
request.headersRetrieves HTTP headers sent by the client.

Example: Process and Display User Form Data

The following example demonstrates how to use Flask's request object to retrieve user-entered data from an HTML form and display it on another webpage. The application works as follows:

  • The user opens a webpage containing a registration form.
  • The user enters their details and clicks Submit.
  • The form sends a POST request to the Flask server.
  • The server retrieves the submitted data using request.form.
  • The data is passed to another HTML page and displayed in a table.

Create a Flask Application and an app.py file.

Python
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def input():
    return render_template('Temp.html')
@app.route('/passing', methods=['GET', 'POST'])
def display():

    if request.method == 'POST':
        result = request.form
        return render_template(
            'result_data.html',
            result=result
        )

if __name__ == '__main__':

    app.run(debug=True)

Explanation:

  • Flask(__name__) creates a Flask application instance.
  • / renders the HTML form page.
  • /passing handles the submitted form data.
  • methods=['GET', 'POST'] allows both request methods.
  • request.method checks whether the request is a POST request.
  • request.form retrieves all submitted form data.
  • render_template() passes the data to result_data.html.

Inside the templates folder, create Temp.html.

HTML
<html>
<style>
body {
    text-align: center;
    background-color: green;
}
form {
    display: inline-block;
}
</style>
<body>
    <h3>Please Fill Out This Form</h3>
    <form action="/passing" method="POST">
        <p>Name <input type="text" name="name" /></p>
        <p>Email <input type="email" name="email" /></p>
        <p>Phone Number <input type="text" name="phone" /></p>
        <p><input type="submit" value="Submit" /></p>
    </form>
</body>
</html>

Explanation:

  • action="https://www.geeksforgeeks.org/passing" sends the form data to the /passing route.
  • method="POST" sends the data as a POST request.
  • Each input field has a name attribute that Flask uses to identify the data.
  • Clicking Submit sends the data to the server.

Inside the templates folder, create result_data.html.

HTML
<!doctype html>
<html>
<style>
body {
    text-align: center;
    background-color: orange;
}
table {
    display: inline-block;
    border-collapse: collapse;
}
</style>
<body>
    <p><strong>Registration Successful</strong></p>
    <table border="1">
        {% for key, value in result.items() %}
        <tr>
            <th>{{ key }}</th>
            <td>{{ value }}</td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>

Explanation:

  • result.items() retrieves all submitted form fields.
  • The {% for %} loop iterates through each key-value pair.
  • <th> displays the field name.
  • <td> displays the corresponding value.
  • The submitted data is displayed in a table format.

Run the Application

Open a terminal and run:

python app.py

Open the following URL in your browser:

http://127.0.0.1:5000/

Output

Screenshot-2026-06-23-144830
Screenshot-2026-06-23-144807
Screenshot-2026-06-23-144813
Comment