Hackforge Academy

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

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community