Extra wrappers or mixins contributed by the community. These wrappers can be mixed in into request objects to add extra functionality.
Example:
from werkzeug import Request as RequestBase
from werkzeug.contrib.wrappers import JSONRequestMixin
class Request(RequestBase, JSONRequestMixin):
pass
Afterwards this request object provides the extra functionality of the JSONRequestMixin.
Add json method to a request object. This will parse the input data through simplejson if possible.
BadRequest will be raised if the content-type is not json or if the data itself cannot be parsed as json.
Add protobuf parsing method to a request object. This will parse the input data through protobuf if possible.
BadRequest will be raised if the content-type is not protobuf or if the data itself cannot be parsed property.
This request mixin adds support for the wsgiorg routing args specification.
This mixin reverses the trailing slash behavior of script_root and path. This makes it possible to use urljoin() directly on the paths.
Because it changes the behavior or Request this class has to be mixed in before the actual request class:
class MyRequest(ReverseSlashBehaviorRequestMixin, Request):
pass
This example shows the differences (for an application mounted on /application and the request going to /application/foo/bar):
normal behavior reverse behavior script_root /application /application/ path /foo/bar foo/bar
“If this mixin is mixed into a request class it will provide a dynamic charset attribute. This means that if the charset is transmitted in the content type headers it’s used from there.
Because it changes the behavior or Request this class has to be mixed in before the actual request class:
class MyRequest(DynamicCharsetRequestMixin, Request):
pass
By default the request object assumes that the URL charset is the same as the data charset. If the charset varies on each request based on the transmitted data it’s not a good idea to let the URLs change based on that. Most browsers assume either utf-8 or latin1 for the URLs if they have troubles figuring out. It’s strongly recommended to set the URL charset to utf-8:
class MyRequest(DynamicCharsetRequestMixin, Request):
url_charset = 'utf-8'
New in version 0.6.
Called if a charset was provided but is not supported by the Python codecs module. By default latin1 is assumed then to not lose any information, you may override this method to change the behavior.
Parameter: | charset – the charset that was not found. |
---|---|
Returns: | the replacement charset. |
If this mixin is mixed into a response class it will provide a dynamic charset attribute. This means that if the charset is looked up and stored in the Content-Type header and updates itself automatically. This also means a small performance hit but can be useful if you’re working with different charsets on responses.
Because the charset attribute is no a property at class-level, the default value is stored in default_charset.
Because it changes the behavior or Response this class has to be mixed in before the actual response class:
class MyResponse(DynamicCharsetResponseMixin, Response):
pass
New in version 0.6.