3. Incoming Request Data¶
Request(environ, populate_request=True, shallow=False)¶
The request object used by default in Flask. Remembers the matched endpoint and view arguments.
The request object is a
Requestsubclass and provides all of the attributes Werkzeug defines plus a few Flask specific ones.
MultiDictwith the parsed form data from
PUTrequests. Please keep in mind that file uploads will not end up here, but instead in the
MultiDictwith the parsed contents of the query string. (The part in the URL after the question mark).
dictwith the contents of all cookies transmitted with the request.
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
datawhich will give you that data as a string. The stream only returns the data once.
The incoming request headers as a dictionary like object.
Contains the incoming request data as string in case it came with a mimetype Flask does not handle.
MultiDictwith files uploaded as part of a
PUTrequest. Each file is stored as
FileStorageobject. It basically behaves like a standard file object you know from Python, with the difference that it also has a
save()function that can store the file on the filesystem.
The underlying WSGI environment.
The current request method (
Provides different ways to look at the current IRI. Imagine your application is listening on the following application root:
And a user requests the following URI:
In this case the values of the above mentioned attributes would be the following:
X-Requested-Withheader and set it to XMLHttpRequest. Libraries that do that are prototype, jQuery and Mochikit and probably some more.
The name of the current blueprint
The endpoint that matched the request. This in combination with
view_argscan be used to reconstruct the same or a modified URL. If an exception happened when matching, this will be
get_json(force=False, silent=False, cache=True)¶
Parses the incoming JSON request data and returns it. By default this function will return
Noneif the mimetype is not application/json but this can be overridden by the
forceparameter. If parsing fails the
on_json_loading_failed()method on the request object will be invoked.
- force – if set to
Truethe mimetype is ignored.
- silent – if set to
Truethis method will fail silently and return
- cache – if set to
Truethe parsed JSON data is remembered on the request.
- force – if set to
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.
If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be
get_json()method should be used instead.
Read-only view of the
The name of the current module if the request was dispatched to an actual module. This is deprecated functionality, use blueprints instead.
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 a
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.
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
NotFoundexception or something similar.
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 (
New in version 0.6.
A dict of view arguments that matched the request. If an exception happened when matching, this will be
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
Requestsubclass and provides all of the attributes Werkzeug defines. This just shows a quick overview of the most important ones.