📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Flask Web Framework Request and Response

Request and Response

5 min read Quiz at the end
Read form data, JSON, query strings, headers, and files from request; return JSON responses.

Request Object

from flask import request, jsonify, make_response

@app.route("/submit", methods=["POST"])
def submit():
    # Form data
    name  = request.form.get("name")
    email = request.form.get("email", "")

    # JSON body
    data  = request.get_json()
    data  = request.json           # shorthand

    # Query string
    page  = request.args.get("page", 1, type=int)
    q     = request.args.get("q", "")

    # Headers
    token = request.headers.get("Authorization")

    # Files
    file  = request.files.get("avatar")

    # Response
    return jsonify({"status": "ok", "name": name}), 201

# Custom response
resp = make_response("Hello", 200)
resp.headers["X-Custom-Header"] = "value"
resp.set_cookie("session_id", "abc123", httponly=True)
return resp
Topic Quiz · 3 questions

Test your understanding before moving on

1. How do you get JSON from a POST request body?
💡 request.get_json() parses the JSON body; request.json is the shorthand.
2. How do you get a query string parameter?
💡 request.args is the query string dict; .get() returns None if missing.
3. Which function creates a JSON response in Flask?
💡 jsonify(data) creates a Response with Content-Type: application/json.