Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Request Object API

Flask Request Object API

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

Import the proxy object via from flask import request and use it directly in view functions.


Common Attributes Quick Reference

property Type Description Example value
method str HTTP Request Methods "GET"、"POST"
path str URL path portion (excluding domain and query string) "/search"
full_path str Path + query string "/search?q=runoob"
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 The matched route rule object Werkzeug Rule instance
view_args dict Dynamic variables in the URL {"post_id": 42}
blueprint str Current blueprint name "auth"、"blog"
blueprints list List of currently matched blueprint levels ["auth"]
is_json bool Whether the request Content-Type is JSON True / False

Get Request Data

Attribute / method Type Description
args MultiDict URL query parameters. Accessing /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 files. request.files["file"] returns a FileStorage object
cookies dict Cookies sent by the client
headers Headers Request headers. 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 errors

locks args、form and other MultiDict types, always use .get(key, default) methods to get values. Direct use of request.args["key"] to access a non-existent key raises a KeyError (returns a 400 error).


Content Negotiation Attributes

Attribute / method Description
accept_mimetypes List of MIME types accepted by the client (MIMEAccept object)
accept_charsets Character sets accepted by the client
accept_encodings Encodings accepted by the client
accept_languages Languages accepted by the client
content_type The request's Content-Type
content_length Request body length (bytes)
content_md5 MD5 checksum of the request body

Code Example

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