Toggle navigation

Quickstart

This part of the documentation shows how to use the most important parts of Werkzeug. It’s intended as a starting point for developers with basic understanding of PEP 333 (WSGI) and RFC 2616 (HTTP).

WSGI Environment

The WSGI environment contains all the information the user request transmits to the application. It is passed to the WSGI application but you can also create a WSGI environ dict using the create_environ() helper:

[UNKNOWN NODE doctest_block]

Now we have an environment to play around:

[UNKNOWN NODE doctest_block]

Usually nobody wants to work with the environ directly because it is limited to bytestrings and does not provide any way to access the form data besides parsing that data by hand.

Enter Request

For access to the request data the Request object is much more fun. It wraps the [UNKNOWN NODE title_reference] and provides a read-only access to the data from there:

[UNKNOWN NODE doctest_block]

Now you can access the important variables and Werkzeug will parse them for you and decode them where it makes sense. The default charset for requests is set to [UNKNOWN NODE title_reference] but you can change that by subclassing Request.

[UNKNOWN NODE doctest_block]

We can also find out which HTTP method was used for the request:

[UNKNOWN NODE doctest_block]

This way we can also access URL arguments (the query string) and data that was transmitted in a POST/PUT request.

For testing purposes we can create a request object from supplied data using the from_values() method:

[UNKNOWN NODE doctest_block]

Now we can access the URL parameters easily:

[UNKNOWN NODE doctest_block]

Same for the supplied form data:

[UNKNOWN NODE doctest_block]

Handling for uploaded files is not much harder as you can see from this example:

def store_file(request):
    file = request.files.get('my_file')
    if file:
        file.save('/where/to/store/the/file.txt')
    else:
        handle_the_error()

The files are represented as FileStorage objects which provide some common operations to work with them.

Request headers can be accessed by using the headers attribute:

[UNKNOWN NODE doctest_block]

The keys for the headers are of course case insensitive.

Header Parsing

There is more. Werkzeug provides convenient access to often used HTTP headers and other request data.

Let’s create a request object with all the data a typical web browser transmits so that we can play with it:

[UNKNOWN NODE doctest_block]

Let’s start with the most useless header: the user agent:

[UNKNOWN NODE doctest_block]

A more useful header is the accept header. With this header the browser informs the web application what mimetypes it can handle and how well. All accept headers are sorted by the quality, the best item being the first:

[UNKNOWN NODE doctest_block]

The same works for languages:

[UNKNOWN NODE doctest_block]

And of course encodings and charsets:

[UNKNOWN NODE doctest_block]

Normalization is available, so you can safely use alternative forms to perform containment checking:

[UNKNOWN NODE doctest_block]

E-tags and other conditional headers are available in parsed form as well:

[UNKNOWN NODE doctest_block]

Responses

Response objects are the opposite of request objects. They are used to send data back to the client. In reality, response objects are nothing more than glorified WSGI applications.

So what you are doing is not returning the response objects from your WSGI application but calling it as WSGI application inside your WSGI application and returning the return value of that call.

So imagine your standard WSGI “Hello World” application:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello World!']

With response objects it would look like this:

from werkzeug.wrappers import Response

def application(environ, start_response):
    response = Response('Hello World!')
    return response(environ, start_response)

Also, unlike request objects, response objects are designed to be modified. So here is what you can do with them:

[UNKNOWN NODE doctest_block]

You can modify the status of the response in the same way. Either just the code or provide a message as well:

[UNKNOWN NODE doctest_block]

As you can see attributes work in both directions. So you can set both status and status_code and the change will be reflected to the other.

Also common headers are exposed as attributes or with methods to set / retrieve them:

[UNKNOWN NODE doctest_block]

Because etags can be weak or strong there are methods to set them:

[UNKNOWN NODE doctest_block]

Some headers are available as mutable structures. For example most of the [UNKNOWN NODE title_reference] headers are sets of values:

[UNKNOWN NODE doctest_block]

Also here this works in both directions:

[UNKNOWN NODE doctest_block]

Authentication headers can be set that way as well:

[UNKNOWN NODE doctest_block]

Cookies can be set as well:

[UNKNOWN NODE doctest_block]

If headers appear multiple times you can use the getlist() method to get all values for a header:

[UNKNOWN NODE doctest_block]

Finally if you have set all the conditional values, you can make the response conditional against a request. Which means that if the request can assure that it has the information already, no data besides the headers is sent over the network which saves traffic. For that you should set at least an etag (which is used for comparison) and the date header and then call make_conditional with the request object.

The response is modified accordingly (status code changed, response body removed, entity headers removed etc.)