100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

The Request and Response Objects

Learn how Flask's request object exposes incoming data (query params, form fields, JSON, files) and how views build responses using tuples, jsonify, and redirects.

Flask FoundationsIntermediate10 min readJul 10, 2026
Analogies

The Request Object

Flask's request object, imported from flask and backed by a thread-local proxy, gives you access to everything about the incoming HTTP request within a view function — request.args for query string parameters, request.form for submitted form fields, request.headers for HTTP headers, and request.cookies for cookies sent by the client — without you having to pass it explicitly into every function.

🏏

Cricket analogy: The request object being globally accessible within a view is like a stump microphone feeding live audio to every commentator in the box simultaneously, without each commentator needing a separate wire run to the pitch.

Accessing Form and JSON Data

For traditional HTML form submissions, request.form["email"] (or the safer request.form.get("email")) retrieves a field, while for JSON APIs request.get_json() parses the request body (must have Content-Type: application/json, or pass force=True) into a Python dict; request.files handles uploaded files as FileStorage objects, and using .get() instead of indexing avoids a KeyError/BadRequest when an expected field is missing.

🏏

Cricket analogy: request.form.get('email') defaulting safely versus indexing is like DRS (Decision Review System) offering an 'umpire's call' fallback instead of crashing the match when the ball-tracking data is inconclusive.

Building Responses

A view function can return a plain string (Flask wraps it in a 200 Response with text/html), a tuple like (body, status_code) or (body, status_code, headers) for finer control, or an explicit flask.Response object; helper functions like jsonify() serialize a dict to JSON with the correct Content-Type: application/json header, and redirect(url_for(...)) combined with a 3xx status sends the browser to a different URL.

🏏

Cricket analogy: Returning (body, status_code) for fine-grained control is like a scorer not just announcing 'four runs' but also specifying it was a boundary through cover, giving the full context in one update.

python
from flask import request, jsonify, redirect, url_for

@app.route("/api/users", methods=["POST"])
def create_user():
    data = request.get_json(force=True)
    email = data.get("email")
    if not email:
        return jsonify(error="email is required"), 400
    return jsonify(id=1, email=email), 201

@app.route("/old-profile")
def old_profile():
    return redirect(url_for("show_profile"), code=301)

Calling request.form['field'] or request.get_json()['field'] with square-bracket indexing raises an exception (400 Bad Request) when the field is missing — prefer .get() with a sensible default for optional fields.

  • The request object exposes args (query string), form (submitted fields), headers, cookies, and files.
  • request.form.get() is safer than indexing since it avoids exceptions on missing fields.
  • request.get_json() parses a JSON body into a Python dict, requiring the correct Content-Type unless force=True.
  • Views can return a string, a (body, status) or (body, status, headers) tuple, or a Response object.
  • jsonify() serializes a dict to JSON and sets Content-Type: application/json automatically.
  • redirect(url_for(...)) sends the client a 3xx response pointing to another route.

Practice what you learned

Was this page helpful?

Topics covered

#Python#FlaskStudyNotes#WebDevelopment#TheRequestAndResponseObjects#Request#Response#Objects#Object#OOP#StudyNotes#SkillVeris