request Object (Flask)

Flask's request object holds all data from the incoming HTTP request — form fields, query-string args, JSON body, headers, cookies, and uploaded files. It is a thread-local global you import from flask.

Flask

The request object is your window into what the client sent. Common attributes are request.args (query string ?q=...), request.form (submitted form fields), request.json (parsed JSON body), request.files (uploads), request.headers, request.cookies, and request.method.

It is a 'context-local' proxy: although you import it once at the top of the file, it transparently refers to the current request being handled, even across concurrent requests. This is why you can use a single global without threads colliding.

Use request.form.get('key') rather than request.form['key'] when a field might be missing, to get None instead of a 400 BadRequest error.

Syntax

from flask import request
request.args.get("q")
request.form["email"]

Examples

Python — /search?q=flask -> 'Searching for: flask'

from flask import request

@app.route("/search")
def search():
    q = request.args.get("q", "")
    return f"Searching for: {q}"

Learn it in a lesson

See also

Back to the Dictionary