Category: python
request and response handling
Published on 30 Sep 2026
Explanation
Flask provides the request object to access information about the incoming HTTP request.
Code:
from flask import request
@app.route('/info')
def info():
return {
'method': request.method,
'path': request.path
}
Explanation
Request headers can be accessed using request.headers.
Code:
@app.route('/headers')
def headers():
return {
'user_agent': request.headers.get('User-Agent')
}
Explanation
Flask automatically converts Python dictionaries and lists into JSON responses when returned from a view.
Code:
@app.route('/user')
def user():
return {
'id': 101,
'name': 'Praveen'
}
Explanation
A response can include both a response body and an HTTP status code.
Code:
@app.route('/created')
def created():
return {'message': 'Created'}, 201
Explanation
make_response() can be used when more control over status codes, headers, and response content is required.
Code:
from flask import make_response
@app.route('/custom')
def custom():
response = make_response('Success', 200)
response.headers['X-App'] = 'Flask'
return response