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.
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
1. Which request attribute holds query string parameters (e.g., ?page=2)?
2. What does request.form.get('email') do differently than request.form['email'] when the field is missing?
3. What must be true for request.get_json() to parse the body without force=True?
4. What does jsonify() set automatically?
5. What does redirect(url_for('show_profile'), code=301) do?
Was this page helpful?
You May Also Like
Routing in Flask
Understand how @app.route binds URLs to view functions, how dynamic URL converters validate input, and how to handle HTTP methods and generate URLs with url_for.
Creating a Flask Application
Learn to set up a Flask project with a virtual environment, understand the central application object, and run the development server safely.
Flask Application Structure
Learn how to organize a growing Flask project using the application factory pattern, Blueprints for modularity, and conventional templates/static/config layouts.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics
Web DevelopmentNext.js Study Notes
JavaScript · 30 topics