3. Incoming Request Data¶
-
class
flask.
Request
(environ, populate_request=True, shallow=False)[source]¶ The request object used by default in Flask. Remembers the matched endpoint and view arguments.
It is what ends up as
request
. If you want to replace the request object used you can subclass this and setrequest_class
to your subclass.The request object is a
Request
subclass and provides all of the attributes Werkzeug defines plus a few Flask specific ones.-
form
¶ A
MultiDict
with the parsed form data fromPOST
orPUT
requests. Please keep in mind that file uploads will not end up here, but instead in thefiles
attribute.
-
args
¶ A
MultiDict
with the parsed contents of the query string. (The part in the URL after the question mark).
A
dict
with the contents of all cookies transmitted with the request.
-
stream
¶ If the incoming form data was not encoded with a known mimetype the data is stored unmodified in this stream for consumption. Most of the time it is a better idea to use
data
which will give you that data as a string. The stream only returns the data once.
-
headers
¶ The incoming request headers as a dictionary like object.
-
data
¶ Contains the incoming request data as string in case it came with a mimetype Flask does not handle.
-
files
¶ A
MultiDict
with files uploaded as part of aPOST
orPUT
request. Each file is stored asFileStorage
object. It basically behaves like a standard file object you know from Python, with the difference that it also has asave()
function that can store the file on the filesystem.
-
environ
¶ The underlying WSGI environment.
-
method
¶ The current request method (
POST
,GET
etc.)
-
path
¶
-
full_path
¶
-
script_root
¶
-
url
¶
-
base_url
¶
-
url_root
¶ Provides different ways to look at the current IRI. Imagine your application is listening on the following application root:
http://www.example.com/myapplication
And a user requests the following URI:
http://www.example.com/myapplication/%CF%80/page.html?x=y
In this case the values of the above mentioned attributes would be the following:
path u'/π/page.html'
full_path u'/π/page.html?x=y'
script_root u'/myapplication'
base_url u'http://www.example.com/myapplication/π/page.html'
url u'http://www.example.com/myapplication/π/page.html?x=y'
url_root u'http://www.example.com/myapplication/'
-
is_xhr
¶ True
if the request was triggered via a JavaScript XMLHttpRequest. This only works with libraries that support theX-Requested-With
header and set it to XMLHttpRequest. Libraries that do that are prototype, jQuery and Mochikit and probably some more.
-
blueprint
¶ The name of the current blueprint
-
endpoint
¶ The endpoint that matched the request. This in combination with
view_args
can be used to reconstruct the same or a modified URL. If an exception happened when matching, this will beNone
.
-
get_json
(force=False, silent=False, cache=True)[source]¶ Parses the incoming JSON request data and returns it. By default this function will return
None
if the mimetype is not application/json but this can be overridden by theforce
parameter. If parsing fails theon_json_loading_failed()
method on the request object will be invoked.Parameters: - force – if set to
True
the mimetype is ignored. - silent – if set to
True
this method will fail silently and returnNone
. - cache – if set to
True
the parsed JSON data is remembered on the request.
- force – if set to
-
is_json
¶ Indicates if this request is JSON or not. By default a request is considered to include JSON data if the mimetype is application/json or application/*+json.
New in version 0.11.
-
json
¶ If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be
None
.The
get_json()
method should be used instead.
-
max_content_length
¶ Read-only view of the
MAX_CONTENT_LENGTH
config key.
-
module
¶ The name of the current module if the request was dispatched to an actual module. This is deprecated functionality, use blueprints instead.
-
on_json_loading_failed
(e)[source]¶ Called if decoding of the JSON data failed. The return value of this method is used by
get_json()
when an error occurred. The default implementation just raises aBadRequest
exception.Changed in version 0.10: Removed buggy previous behavior of generating a random JSON response. If you want that behavior back you can trivially add it by subclassing.
New in version 0.8.
-
routing_exception
= None¶ If matching the URL failed, this is the exception that will be raised / was raised as part of the request handling. This is usually a
NotFound
exception or something similar.
-
url_rule
= None¶ The internal URL rule that matched the request. This can be useful to inspect which methods are allowed for the URL from a before/after handler (
request.url_rule.methods
) etc.New in version 0.6.
-
view_args
= None¶ A dict of view arguments that matched the request. If an exception happened when matching, this will be
None
.
-
-
class
flask.
request
¶ To access incoming request data, you can use the global request object. Flask parses incoming request data for you and gives you access to it through that global object. Internally Flask makes sure that you always get the correct data for the active thread if you are in a multithreaded environment.
This is a proxy. See Notes On Proxies for more information.
The request object is an instance of a
Request
subclass and provides all of the attributes Werkzeug defines. This just shows a quick overview of the most important ones.