Kay uses the request/response objects of Werkzeug which is WSGI-compliant. When accessed by browsers Kay creates request object and pass it to the view function specified by URL mapping. Views have to take the request object as the first argument, create an response object, and return it. In this chapter, we explain about the structure of request/response objects.
Full featured request object implementing the following mixins:
If authentication is enabled this is the user object specified in the AUTH_USER_MODEL of settings.py
See also
If session is enabled this is the session data.
See also
Decorate a function as responder that accepts the request as first argument. This works like the responder() decorator but the function is passed the request object as first argument:
@Request.application
def my_wsgi_app(request):
return Response('Hello World!')
| Parameter: | f – the WSGI callable to decorate |
|---|---|
| Returns: | a new WSGI callable |
This reads the buffered incoming data from the client into the string. Usually it’s a bad idea to access data because a client could send dozens of megabytes or more to cause memory problems on the server.
To circumvent that make sure to check the content length first.
MultiDict object containing all uploaded files. Each key in files is the name from the <input type="file" name="">. Each value in files is a Werkzeug FileStorage object.
Note that files will only contain data if the request method was POST or PUT and the <form> that posted to the request had enctype="multipart/form-data". It will be empty otherwise.
See the MultiDict / FileStorage documentation for more details about the used data structure.
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 (Client) that allows to create multipart requests, support for cookies etc.
This accepts the same options as the EnvironBuilder.
Changed in version 0.5: This method now accepts the same arguments as EnvironBuilder. Because of this the environ parameter is now called environ_overrides.
| Returns: | request object |
|---|
The charset that is assumed for URLs. Defaults to the value of charset.
New in version 0.6.
Full featured response object implementing the following mixins:
The Age response-header field conveys the sender’s estimate of the amount of time since the response (or its revalidation) was generated at the origin server.
Age values are non-negative decimal integers, representing time in seconds.
Adds a function to the internal list of functions that should be called as part of closing down the response.
New in version 0.6.
The string representation of the request body. Whenever you access this property the request iterable is encoded and flattened. This can lead to unwanted behavior if you stream big data.
This behavior can be disabled by setting implicit_seqence_conversion to False.
Delete a cookie. Fails silently if key doesn’t exist.
| Parameters: |
|
|---|
Enforce that the WSGI response is a response object of the current type. Werkzeug will use the BaseResponse internally in many situations like the exceptions. If you call get_response() on an exception you will get back a regular BaseResponse object, even if you are using a custom subclass.
This method can enforce a given response type, and it will also convert arbitrary WSGI callables into response objects if an environ is provided:
# convert a Werkzeug response object into an instance of the
# MyResponseClass subclass.
response = MyResponseClass.force_type(response)
# convert any WSGI application into a response object
response = MyResponseClass.force_type(response, environ)
This is especially useful if you want to post-process responses in the main dispatcher and use functionality provided by your subclass.
Keep in mind that this will modify response objects in place if possible!
| Parameters: |
|
|---|---|
| Returns: | a response object. |
Call this method if you want to make your response object ready for being pickled. This buffers the generator if there is one. It will also set the Content-Length header to the length of the body.
Changed in version 0.6: The Content-Length header is now set.
Create a new response object from an application output. This works best if you pass it an application that returns a generator all the time. Sometimes applications may use the write() callable returned by the start_response function. This tries to resolve such edge cases automatically. But if you don’t get the expected output you should set buffered to True which enforces buffering.
| Parameters: |
|
|---|---|
| Returns: | a response object. |
Returns the application iterator for the given environ. Depending on the request method and the current status code the return value might be an empty response rather than the one from the response.
If the request method is HEAD or the status code is in a range where the HTTP specification requires an empty response, an empty iterable is returned.
New in version 0.6.
| Parameter: | environ – the WSGI environment of the request. |
|---|---|
| Returns: | a response iterable. |
This is automatically called right before the response is started and returns headers modified for the given environment. It returns a copy of the headers from the response with some modifications applied if necessary.
For example the location header (if present) is joined with the root URL of the environment. Also the content length is automatically set to zero here for certain status codes.
Changed in version 0.6: Previously that function was called fix_headers and modified the response object in place. Also since 0.6, IRIs in location and content-location headers are handled properly.
Also starting with 0.6, Werkzeug will attempt to set the content length if it is able to figure it out on its own. This is the case if all the strings in the response iterable are already encoded and the iterable is buffered.
| Parameter: | environ – the WSGI environment of the request. |
|---|---|
| Returns: | returns a new Headers object. |
Returns the final WSGI response as tuple. The first item in the tuple is the application iterator, the second the status and the third the list of headers. The response returned is created specially for the given environment. For example if the request method in the WSGI environment is 'HEAD' the response will be empty and only the headers and status code will be present.
New in version 0.6.
| Parameter: | environ – the WSGI environment of the request. |
|---|---|
| Returns: | an (app_iter, status, headers) tuple. |
If the iterator is buffered, this property will be True. A response object will consider an iterator to be buffered if the response attribute is a list or tuple.
New in version 0.6.
If the response is streamed (the response is not an iterable with a length information) this property is True. In this case streamed means that there is no information about the number of iterations. This is usually True if a generator is passed to the response object.
This is useful for checking before applying some sort of post filtering that should not take place for streamed responses.
Iter the response encoded with the encoding of the response. If the response object is invoked as WSGI application the return value of this method is used as application iterator unless direct_passthrough was activated.
Changed in version 0.6.
Make the response conditional to the request. This method works best if an etag was defined for the response already. The add_etag method can be used to do that. If called without etag just the date header is set.
This does nothing if the request method in the request or environ is anything but GET or HEAD.
It does not remove the body of the response because that’s something the __call__() function does for us automatically.
Returns self so that you can do return resp.make_conditional(req) but modifies the object in-place.
| Parameter: | request_or_environ – a request object or WSGI environment to be used to make the response conditional against. |
|---|
Converts the response iterator in a list. By default this happens automatically if required. If implicit_seqence_conversion is disabled, this method is not automatically called and some properties might raise exceptions. This also encodes all the items.
New in version 0.6.
The mimetype parameters as dict. For example if the content type is text/html; charset=utf-8 the params would be {'charset': 'utf-8'}.
New in version 0.5.
The Retry-After response-header field can be used with a 503 (Service Unavailable) response to indicate how long the service is expected to be unavailable to the requesting client.
Time in seconds until expiration or date.
Sets a cookie. The parameters are the same as in the cookie Morsel object in the Python standard library but it accepts unicode data, too.
| Parameters: |
|
|---|
There are various exceptions in :mod:werkzeug.exceptions. Each exception’s name represents which type of HTTP Error. You can raise these exceptions when you want return such errors to the users.
Here are the list of exceptions: