Flask Request and Response Tutorial


This tutorial explains how to handle request data in Flask. We will cover accessing request data, working with query parameters, and handling form data using real examples.

Step 1: Setting Up Flask

First, ensure you have Flask installed. You can install it using pip:

pip install flask

Create a new Python file, for example, app.py, and import Flask and the request object:

    from flask import Flask, request

    app = Flask(__name__)
        

Step 2: Accessing Request Data (GET and POST)

Flask allows you to access request data using the request object. Here’s an example of handling both GET and POST requests:

    @app.route('/submit', methods=['GET', 'POST'])
    def handle_request():
        if request.method == 'GET':
            return "Received a GET request"
        elif request.method == 'POST':
            return "Received a POST request"
        

This endpoint will respond differently based on the request method.

Step 3: Working with Query Parameters

Query parameters are part of the URL. For example, http://example.com/?name=John&age=25. You can access them using request.args:

    @app.route('/greet', methods=['GET'])
    def greet_user():
        name = request.args.get('name', 'Guest')
        age = request.args.get('age', 'unknown')
        return f"Hello {name}, you are {age} years old!"
        

When you visit /greet?name=John&age=25, the server will respond with: "Hello John, you are 25 years old!"

Step 4: Handling Form Data

Form data is typically sent via POST requests. You can access form fields using request.form. Here’s an example:

    @app.route('/form', methods=['POST'])
    def handle_form():
        username = request.form.get('username')
        password = request.form.get('password')
        return f"Received username: {username} and password: {password}"
        

Use the following HTML form to send data to the /form endpoint:

    


Step 5: Running the Application

Run your Flask app by executing the following command:

python app.py

Access the endpoints by navigating to http://127.0.0.1:5000 in your browser or using tools like Postman.

Complete Example

Below is the complete Flask application combining all the examples above:

    from flask import Flask, request

    app = Flask(__name__)

    @app.route('/submit', methods=['GET', 'POST'])
    def handle_request():
        if request.method == 'GET':
            return "Received a GET request"
        elif request.method == 'POST':
            return "Received a POST request"

    @app.route('/greet', methods=['GET'])
    def greet_user():
        name = request.args.get('name', 'Guest')
        age = request.args.get('age', 'unknown')
        return f"Hello {name}, you are {age} years old!"

    @app.route('/form', methods=['POST'])
    def handle_form():
        username = request.form.get('username')
        password = request.form.get('password')
        return f"Received username: {username} and password: {password}"

    if __name__ == '__main__':
        app.run(debug=True)
        

Conclusion

In this tutorial, we learned how to handle requests in Flask, including accessing GET and POST data, working with query parameters, and processing form data. You can now extend these examples to build more complex Flask applications.





Advertisement