class documentation

Represents an incoming WSGI HTTP request, with headers and body taken from the WSGI environment. Has properties and methods for using the functionality defined by various HTTP specs. The data in requests object is read-only. Text data is assumed to use UTF-8 encoding, which should be true for the vast majority of modern clients. Using an encoding set by the client is unsafe in Python due to extra encodings it provides, such as ``zip``. To change the assumed encoding, subclass and replace :attr:`charset`. :param environ: The WSGI environ is generated by the WSGI server and contains information about the server configuration and client request. :param populate_request: Add this request object to the WSGI environ as ``environ['werkzeug.request']``. Can be useful when debugging. :param shallow: Makes reading from :attr:`stream` (and any method that would read from it) raise a :exc:`RuntimeError`. Useful to prevent consuming the form data in middleware, which would make it unavailable to the final application. .. versionchanged:: 2.1 Remove the ``disable_data_descriptor`` attribute. .. versionchanged:: 2.0 Combine ``BaseRequest`` and mixins into a single ``Request`` class. Using the old classes is deprecated and will be removed in Werkzeug 2.1. .. versionchanged:: 0.5 Read-only mode is enforced with immutable classes for all data.

Class Method application Decorate a function as responder that accepts the request as the last argument. This works like the :func:`responder` decorator but the function is passed the request object as the last argument and the request object will be closed automatically::...
Class Method from_values Create a new request object based on the values provided. If environ is given missing values are filled from there. This method is useful for small scripts when you need to simulate a request from an URL...
Method __enter__ Undocumented
Method __exit__ Undocumented
Method __init__ Undocumented
Method close Closes associated resources of this request object. This closes all file handles explicitly. You can also use the request object in a with statement which will automatically close it.
Method get_data This reads the buffered incoming data from the client into one bytes object. By default this is cached but that behavior can be changed by setting `cache` to `False`.
Method get_json Parse :attr:`data` as JSON.
Method make_form_data_parser Creates the form data parser. Instantiates the :attr:`form_data_parser_class` with some parameters.
Method on_json_loading_failed Called if :meth:`get_json` fails and isn't silenced.
Class Variable input_stream Undocumented
Class Variable is_multiprocess Undocumented
Class Variable is_multithread Undocumented
Class Variable is_run_once Undocumented
Class Variable max_content_length Undocumented
Class Variable max_form_memory_size Undocumented
Class Variable max_form_parts Undocumented
Class Variable remote_user Undocumented
Instance Variable environ Undocumented
Instance Variable shallow Undocumented
Property data Contains the incoming request data as string in case it came with a mimetype Werkzeug does not handle.
Property files :class:`~werkzeug.datastructures.MultiDict` object containing all uploaded files. Each key in :attr:`files` is the name from the ``<input type="file" name="">``. Each value in :attr:`files` is a Werkzeug :class:`~werkzeug...
Property form The form parameters. By default an :class:`~werkzeug.datastructures.ImmutableMultiDict` is returned from this function. This can be changed by setting :attr:`parameter_storage_class` to a different type...
Property json The parsed JSON data if :attr:`mimetype` indicates JSON (:mimetype:`application/json`, see :attr:`is_json`).
Property script_root Alias for :attr:`self.root_path`. ``environ["SCRIPT_ROOT"]`` without a trailing slash.
Property 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 :attr:`data` which will give you that data as a string...
Property url_root Alias for :attr:`root_url`. The URL with scheme, host, and root path. For example, ``https://example.com/app/``.
Property values A :class:`werkzeug.datastructures.CombinedMultiDict` that combines :attr:`args` and :attr:`form`.
Property want_form_data_parsed ``True`` if the request method carries content. By default this is true if a ``Content-Type`` is sent.
Method _get_file_stream Called to get a stream for the file upload.
Method _get_stream_for_parsing This is the same as accessing :attr:`stream` with the difference that if it finds cached data from calling :meth:`get_data` first it will create a new stream out of the cached data.
Method _load_form_data Method used internally to retrieve submitted data. After calling this sets `form` and `files` on the request object to multi dicts filled with the incoming form data. As a matter of fact the input stream will be empty afterwards...
Class Variable _cached_json Undocumented
Instance Variable _cached_data Undocumented

Inherited from Request:

Method __repr__ Undocumented
Class Variable access_control_request_headers Undocumented
Class Variable access_control_request_method Undocumented
Class Variable charset Undocumented
Class Variable content_encoding Undocumented
Class Variable content_md5 Undocumented
Class Variable content_type Undocumented
Class Variable date Undocumented
Class Variable encoding_errors The class used and returned by the :attr:`user_agent` property to parse the header. Defaults to :class:`~werkzeug.user_agent.UserAgent`, which does no parsing. An extension can provide a subclass that uses a parser to provide other data.
Class Variable max_forwards Undocumented
Class Variable origin Undocumented
Class Variable referrer Undocumented
Class Variable trusted_hosts Undocumented
Instance Variable headers Undocumented
Instance Variable method Undocumented
Instance Variable path Undocumented
Instance Variable query_string Undocumented
Instance Variable remote_addr Undocumented
Instance Variable root_path Undocumented
Instance Variable scheme Undocumented
Instance Variable server Undocumented
Property accept_charsets List of charsets this client supports as :class:`~werkzeug.datastructures.CharsetAccept` object.
Property accept_encodings List of encodings this client accepts. Encodings in a HTTP term are compression encodings such as gzip. For charsets have a look at :attr:`accept_charset`.
Property accept_languages List of languages this client accepts as :class:`~werkzeug.datastructures.LanguageAccept` object.
Property accept_mimetypes List of mimetypes this client supports as :class:`~werkzeug.datastructures.MIMEAccept` object.
Property access_route If a forwarded header exists this is a list of all ip addresses from the client ip to the last proxy server.
Property args The parsed URL parameters (the part in the URL after the question mark).
Property authorization The `Authorization` object in parsed form.
Property base_url Like :attr:`url` but without the query string.
Property cache_control A :class:`~werkzeug.datastructures.RequestCacheControl` object for the incoming cache control headers.
Property content_length The Content-Length entity-header field indicates the size of the entity-body in bytes or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.
Property cookies A :class:`dict` with the contents of all cookies transmitted with the request.
Property full_path Requested path, including the query string.
Property host The host name the request was made to, including the port if it's non-standard. Validated with :attr:`trusted_hosts`.
Property host_url The request URL scheme and host only.
Property if_match An object containing all the etags in the `If-Match` header.
Property if_modified_since The parsed `If-Modified-Since` header as a datetime object.
Property if_none_match An object containing all the etags in the `If-None-Match` header.
Property if_range The parsed ``If-Range`` header.
Property if_unmodified_since The parsed `If-Unmodified-Since` header as a datetime object.
Property is_json Check if the mimetype indicates JSON data, either :mimetype:`application/json` or :mimetype:`application/*+json`.
Property is_secure ``True`` if the request was made with a secure protocol (HTTPS or WSS).
Property mimetype Like :attr:`content_type`, but without parameters (eg, without charset, type etc.) and always lowercase. For example if the content type is ``text/HTML; charset=utf-8`` the mimetype would be ``'text/html'``.
Property mimetype_params The mimetype parameters as dict. For example if the content type is ``text/html; charset=utf-8`` the params would be ``{'charset': 'utf-8'}``.
Property pragma The Pragma general-header field is used to include implementation-specific directives that might apply to any recipient along the request/response chain. All pragma directives specify optional behavior from the viewpoint of the protocol; however, some systems MAY require that behavior be consistent with the directives.
Property range The parsed `Range` header.
Property root_url The request URL scheme, host, and root path. This is the root that the application is accessed from.
Property url The full request URL with the scheme, host, root path, path, and query string.
Property url_charset The charset that is assumed for URLs. Defaults to the value of :attr:`charset`.
Property user_agent The user agent. Use ``user_agent.string`` to get the header value. Set :attr:`user_agent_class` to a subclass of :class:`~werkzeug.user_agent.UserAgent` to provide parsing for the other properties or other extended data.
Method _parse_content_type Undocumented
Instance Variable _parsed_content_type Undocumented
@classmethod
def application(cls, f: t.Callable[[Request], WSGIApplication]) -> WSGIApplication: (source)

Decorate a function as responder that accepts the request as the last argument. This works like the :func:`responder` decorator but the function is passed the request object as the last argument and the request object will be closed automatically:: @Request.application def my_wsgi_app(request): return Response('Hello World!') As of Werkzeug 0.14 HTTP exceptions are automatically caught and converted to responses instead of failing. :param f: the WSGI callable to decorate :return: a new WSGI callable

@classmethod
def from_values(cls, *args: t.Any, **kwargs: t.Any) -> Request: (source)

Create a new request object based on the values provided. If environ is given missing values are filled from there. This method is useful for small scripts when you need to simulate a request from an URL. Do not use this method for unittesting, there is a full featured client object (:class:`Client`) that allows to create multipart requests, support for cookies etc. This accepts the same options as the :class:`~werkzeug.test.EnvironBuilder`. .. versionchanged:: 0.5 This method now accepts the same arguments as :class:`~werkzeug.test.EnvironBuilder`. Because of this the `environ` parameter is now called `environ_overrides`. :return: request object

def __enter__(self) -> Request: (source)

Undocumented

def __exit__(self, exc_type, exc_value, tb): (source)

Undocumented

def __init__(self, environ: WSGIEnvironment, populate_request: bool = True, shallow: bool = False): (source)
def close(self): (source)

Closes associated resources of this request object. This closes all file handles explicitly. You can also use the request object in a with statement which will automatically close it. .. versionadded:: 0.9

@typing.overload
def get_data(self, cache: bool = True, as_text: te.Literal[False] = False, parse_form_data: bool = False) -> bytes:
@typing.overload
def get_data(self, cache: bool = True, as_text: te.Literal[True] = ..., parse_form_data: bool = False) -> str:
(source)

This reads the buffered incoming data from the client into one bytes object. By default this is cached but that behavior can be changed by setting `cache` to `False`. Usually it's a bad idea to call this method without checking the content length first as a client could send dozens of megabytes or more to cause memory problems on the server. Note that if the form data was already parsed this method will not return anything as form data parsing does not cache the data like this method does. To implicitly invoke form data parsing function set `parse_form_data` to `True`. When this is done the return value of this method will be an empty string if the form parser handles the data. This generally is not necessary as if the whole data is cached (which is the default) the form parser will used the cached data to parse the form data. Please be generally aware of checking the content length first in any case before calling this method to avoid exhausting server memory. If `as_text` is set to `True` the return value will be a decoded string. .. versionadded:: 0.9

@t.overload
def get_json(self, force: bool = ..., silent: te.Literal[False] = ..., cache: bool = ...) -> t.Any:
@t.overload
def get_json(self, force: bool = ..., silent: bool = ..., cache: bool = ...) -> t.Optional[t.Any]:
(source)

Parse :attr:`data` as JSON. If the mimetype does not indicate JSON (:mimetype:`application/json`, see :attr:`is_json`), or parsing fails, :meth:`on_json_loading_failed` is called and its return value is used as the return value. By default this raises a 400 Bad Request error. :param force: Ignore the mimetype and always try to parse JSON. :param silent: Silence mimetype and parsing errors, and return ``None`` instead. :param cache: Store the parsed JSON to return for subsequent calls. .. versionchanged:: 2.1 Raise a 400 error if the content type is incorrect.

def make_form_data_parser(self) -> FormDataParser: (source)

Creates the form data parser. Instantiates the :attr:`form_data_parser_class` with some parameters. .. versionadded:: 0.8

def on_json_loading_failed(self, e: t.Optional[ValueError]) -> t.Any: (source)

Called if :meth:`get_json` fails and isn't silenced. If this method returns a value, it is used as the return value for :meth:`get_json`. The default implementation raises :exc:`~werkzeug.exceptions.BadRequest`. :param e: If parsing failed, this is the exception. It will be ``None`` if the content type wasn't ``application/json``.

input_stream = (source)

Undocumented

is_multiprocess = (source)

Undocumented

is_multithread = (source)

Undocumented

is_run_once = (source)

Undocumented

max_content_length: t.Optional[int] = (source)

Undocumented

max_form_memory_size: t.Optional[int] = (source)

Undocumented

max_form_parts: int = (source)

Undocumented

remote_user = (source)

Undocumented

Undocumented

Undocumented

Contains the incoming request data as string in case it came with a mimetype Werkzeug does not handle.

:class:`~werkzeug.datastructures.MultiDict` object containing all uploaded files. Each key in :attr:`files` is the name from the ``<input type="file" name="">``. Each value in :attr:`files` is a Werkzeug :class:`~werkzeug.datastructures.FileStorage` object. It basically behaves like a standard file object you know from Python, with the difference that it also has a :meth:`~werkzeug.datastructures.FileStorage.save` function that can store the file on the filesystem. Note that :attr:`files` will only contain data if the request method was POST, PUT or PATCH and the ``<form>`` that posted to the request had ``enctype="multipart/form-data"``. It will be empty otherwise. See the :class:`~werkzeug.datastructures.MultiDict` / :class:`~werkzeug.datastructures.FileStorage` documentation for more details about the used data structure.

The form parameters. By default an :class:`~werkzeug.datastructures.ImmutableMultiDict` is returned from this function. This can be changed by setting :attr:`parameter_storage_class` to a different type. This might be necessary if the order of the form data is important. Please keep in mind that file uploads will not end up here, but instead in the :attr:`files` attribute. .. versionchanged:: 0.9 Previous to Werkzeug 0.9 this would only contain form data for POST and PUT requests.

The parsed JSON data if :attr:`mimetype` indicates JSON (:mimetype:`application/json`, see :attr:`is_json`). Calls :meth:`get_json` with default arguments. If the request content type is not ``application/json``, this will raise a 400 Bad Request error. .. versionchanged:: 2.1 Raise a 400 error if the content type is incorrect.

@property
script_root: str = (source)

Alias for :attr:`self.root_path`. ``environ["SCRIPT_ROOT"]`` without a trailing slash.

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 :attr:`data` which will give you that data as a string. The stream only returns the data once. Unlike :attr:`input_stream` this stream is properly guarded that you can't accidentally read past the length of the input. Werkzeug will internally always refer to this stream to read data which makes it possible to wrap this object with a stream that does filtering. .. versionchanged:: 0.9 This stream is now always available but might be consumed by the form parser later on. Previously the stream was only set if no parsing happened.

Alias for :attr:`root_url`. The URL with scheme, host, and root path. For example, ``https://example.com/app/``.

A :class:`werkzeug.datastructures.CombinedMultiDict` that combines :attr:`args` and :attr:`form`. For GET requests, only ``args`` are present, not ``form``. .. versionchanged:: 2.0 For GET requests, only ``args`` are present, not ``form``.

@property
want_form_data_parsed: bool = (source)

``True`` if the request method carries content. By default this is true if a ``Content-Type`` is sent. .. versionadded:: 0.8

def _get_file_stream(self, total_content_length: t.Optional[int], content_type: t.Optional[str], filename: t.Optional[str] = None, content_length: t.Optional[int] = None) -> t.IO[bytes]: (source)

Called to get a stream for the file upload. This must provide a file-like class with `read()`, `readline()` and `seek()` methods that is both writeable and readable. The default implementation returns a temporary file if the total content length is higher than 500KB. Because many browsers do not provide a content length for the files only the total content length matters. :param total_content_length: the total content length of all the data in the request combined. This value is guaranteed to be there. :param content_type: the mimetype of the uploaded file. :param filename: the filename of the uploaded file. May be `None`. :param content_length: the length of this file. This value is usually not provided because webbrowsers do not provide this value.

def _get_stream_for_parsing(self) -> t.IO[bytes]: (source)

This is the same as accessing :attr:`stream` with the difference that if it finds cached data from calling :meth:`get_data` first it will create a new stream out of the cached data. .. versionadded:: 0.9.3

def _load_form_data(self): (source)

Method used internally to retrieve submitted data. After calling this sets `form` and `files` on the request object to multi dicts filled with the incoming form data. As a matter of fact the input stream will be empty afterwards. You can also call this method to force the parsing of the form data. .. versionadded:: 0.8

Undocumented

_cached_data = (source)

Undocumented