Flask Request Object API

The Request class encapsulates all information about the HTTP request sent by the client.

Throughfrom flask import requestImport the proxy object and use it directly in view functions.


Common Attributes Quick Reference

Property Type Description Example value
method str HTTP request method "GET"、"POST"
path str URL path part (excluding domain and query string) "/search"
full_path str Path + query string "/search?q=example"
url str Full request URL (excluding query string) "http://localhost:5000/search"
base_url str Same as url, with the trailing query string removed. "http://localhost:5000/search"
url_root str Domain + application root path "http://localhost:5000/"
host str Host header, including port "localhost:5000"
host_url str scheme + host + application root "http://localhost:5000/"
scheme str Protocol "http"、"https"
remote_addr str Client IP address "127.0.0.1"
endpoint str The matched route endpoint "index"、"blog.show"
url_rule Rule Matched route rule object Werkzeug Rule Instance
view_args dict Dynamic variables in URL {"post_id": 42}
blueprint str Current Blueprint Name "auth"、"blog"
blueprints list Current matched blueprint level list ["auth"]
is_json bool Whether the request Content-Type is JSON True / False

Get Request Data

Attributes/Methods Type Description
args MultiDict URL query parameters. Access /search?q=flask → request.args.get("q")
form MultiDict POST form data (application/x-www-form-urlencoded or multipart/form-data)
json dict / None JSON request body (parsed into a Python object). Returns None when the request is not JSON.
data bytes Raw Request Body Data
files FileMultiDict Uploaded file. request.files["file"] returns a FileStorage object
cookies dict Cookie sent by the client
headers Headers Request header. Supports multiple access methods
get_json(force=False, silent=False, cache=True) — Parse the JSON request body. force forces parsing of non-JSON types, silent returns None on error

Pairargs、formetc. MultiDict types, always use..get(key, default)Method to get value. Use directlyrequest.args["key"]Accessing a non-existent key raises KeyError (returns a 400 error).


Content Negotiation Attributes

Attributes/Methods Description
accept_mimetypes List of MIME types accepted by the client (MIMEAccept object).
accept_charsets Client-accepted character set
accept_encodings Client-accepted encoding
accept_languages Client-accepted language
content_type Request's Content-Type
content_length Request body length (bytes)
content_md5 MD5 checksum of the request body

Code Examples

Example

from flask import Flask, request

app = Flask(__name__)

@app.route("/demo", methods=["GET", "POST"])
def demo():
    # Get Query Parameters
    keyword = request.args.get("q", "")

    # Get Form Data
    username = request.form.get("username", "")

    # Get JSON Data
    if request.is_json:
        data = request.json
        name = data.get("name", "unknown")

    # Get Request Headers
    user_agent = request.headers.get("User-Agent", "")
    accept_lang = request.accept_languages.best

    # Get URL Information
    url_info = {
        "method": request.method,       # "GET" or "POST"
        "path": request.path,           # "/demo"
        "url": request.url,             # Full URL
        "remote_addr": request.remote_addr,  # Client IP
    }
    return url_info
other extensions