File: request.rst

package info (click to toggle)
pesto 25-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 516 kB
  • ctags: 519
  • sloc: python: 3,062; makefile: 107
file content (151 lines) | stat: -rw-r--r-- 4,527 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

The request object
====================

.. testsetup:: *
        
        from pesto.request import Request
        from pesto.testing import make_environ, TestApp

The ``request`` object gives access to all WSGI environment properties,
including request headers and server variables, as well as 
submitted form data. 

Core WSGI environment variables and other useful information is exposed as
attributes::

    >>> from pesto.request import Request
    >>> environ = {
    ...     'PATH_INFO': '/pages/index.html',
    ...     'SCRIPT_NAME': '/myapp',
    ...     'REQUEST_METHOD': 'GET',
    ...     'SERVER_PROTOCOL': 'http',
    ...     'SERVER_NAME': 'example.org',
    ...     'SERVER_PORT': '80'
    ... }
    >>> request = Request(environ)
    >>> request.script_name
    '/myapp'
    >>> request.path_info
    '/pages/index.html'
    >>> request.application_uri
    'http://example.org/myapp'

The API documentation at the end of this page contains the complete list of request attributes.

MultiDicts
----------

Many items of data accessible through the request object are exposed as
instances of ``pesto.utils.MultiDict``. This has a dictionary-like interface,
with additional methods for retrieving data where a key has multiple values
associated with it. For example:

.. doctest::

        >>> from pesto.testing import TestApp, make_environ
        >>> from pesto.request import Request
        >>> request = Request(make_environ(QUERY_STRING='a=Trinidad;b=Mexico;b=Honolulu'))
        >>> request.form.get('a')
        u'Trinidad'
        >>> request.form.get('b')
        u'Mexico'
        >>> request.form.getlist('b')
        [u'Mexico', u'Honolulu']

Form data
-----------

Form values are accessible from ``request.form``, a ``pesto.utils.MultiDict``
object, giving dictionary like access to submitted form data::

    request.form["email"]
    request.form.get("email")

As this is a very common usage, shortcuts exist::

    request["email"]
    request.get("email")

For GET requests, ``request.form`` contains data parsed from the URL query string. 
For POST requests, ``request.form`` contains only POSTED data. If a query
string was present in a POST request, it is necessary to use ``request.query``
to access this, which has the same interface.

Cookie data
------------

Cookies are accessible from the ``request.cookies`` MultiDict. See :doc:`cookies` for more information.

File uploads
-------------

The ``request.files`` dictionary has the same interface as ``request.form``,
but values are ``FileUpload`` objects, which allow you to access information
about uploaded files as well as the raw data:

.. testcode::
        :hide:

        >>> from pesto.testing import TestApp
        >>> request = MockWSGI.make_post_multipart(
        ...    files=[('fileupload', 'uploaded.txt', 'text/plain', 'Here is a file upload for you')]
        ... ).request

.. doctest::

    >>> upload = request.files['fileupload']
    >>> upload.filename
    'uploaded.txt'
    >>> upload.file # doctest: +ELLIPSIS
    <cStringIO.StringO object at ...>
    >>> upload.headers # doctest: +ELLIPSIS
    <pesto.httputils.HTTPMessage instance at ...>
    >>> upload.headers['Content-Type']
    'text/plain'
    >>> upload.file.read()
    'Here is a nice file upload for you'

Maximum size limit
------------------

Posted data, including file uploads, is limited in size. This limit can be
altered by adjusting ``pesto.request.Request.MAX_SIZE`` and
``pesto.request.Request.MAX_MULTIPART_SIZE``.

Example 1 – set the global maximum size for POSTed data to 100kb:

.. doctest::

        >>> from pesto.request import Request
        >>> kb = 1024
        >>> Request.MAX_SIZE = 100 * kb

Example 2 – set the global maximum size for multipart/form-data POSTs to 4Mb.

The total size data uploaded, including all form fields and file uploads will
be limited to 4Mb. Individual fields (except file uploads) will be limited to
100Kb by the ``MAX_SIZE`` set in the previous example:

.. doctest::

        >>> Mb = 1024 * kb
        >>> Request.MAX_MULTIPART_SIZE = 4 * Mb

Pesto also supports overriding these limits on a per-request basis:

.. doctest::

        >>> def big_file_upload(request):
        ...     request.MAX_MULTIPART_SIZE = 100 * Mb
        ...     request.files['bigfile'].save('/tmp/bigfile.bin')
        ...     return Response(["Thanks for uploading a big file"])
        ...


pesto.request API documention
-------------------------------

.. automodule:: pesto.request
        :members: