0
0
Flaskframework~5 mins

Why understanding request-response matters in Flask

Choose your learning style9 modes available
Introduction

Understanding request-response helps you know how web apps talk to users. It shows how data moves back and forth.

When building a website that shows different pages based on user clicks.
When creating a form that sends data to a server and gets a reply.
When making an API that other apps use to get or send information.
When debugging why a web page does not load or update correctly.
Syntax
Flask
from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        data = request.form.get('data', '')
        return f'Received: {data}'
    return 'Send a POST request with data.'

request holds what the user sends to the server.

return sends the response back to the user.

Examples
Simple response to a GET request showing a message.
Flask
from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello, world!'
Handles a POST request with form data and replies with a greeting.
Flask
from flask import Flask, request

app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    name = request.form.get('name', 'Guest')
    return f'Hello, {name}!'
Sample Program

This Flask app shows a form on GET. When you type a message and send it (POST), it replies showing what you sent.

Flask
from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        user_input = request.form.get('message', '')
        return f'You sent: {user_input}'
    return '''
        <form method="post">
            <input name="message" placeholder="Type something">
            <button type="submit">Send</button>
        </form>
    '''

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

Every web interaction starts with a request and ends with a response.

Flask makes it easy to read requests and send responses.

Knowing this helps you build interactive web apps that react to user input.

Summary

Request-response is the basic way web apps and browsers communicate.

Understanding it helps you handle user data and show results.

Flask uses request to get data and return to send answers.