Project

General

Profile

Request » History » Version 4

Elmer de Looff, 2012-04-27 12:00
Query arguments doc'd

1 1 Jan Klopper
h1. Request
2 1 Jan Klopper
3 3 Elmer de Looff
The @Request@ object is an abstraction of the incoming HTTP request. This allows one simple interface that is independent of the underlying server that µWeb runs on (either [[Standalone]] using BaseHTTPServer, or [[Apache]] mode on @mod_python@).
4 1 Jan Klopper
5 4 Elmer de Looff
From PageMaker methods, the request object is accessible as the @self.req@ member. The request object contains all the information about the incoming request: query arguments, post data, cookies and environment data. It is also the object where you define cookies that need to be provided to the client.
6 1 Jan Klopper
7 4 Elmer de Looff
h1. Query arguments
8 4 Elmer de Looff
9 4 Elmer de Looff
All query arguments provided by the client are present on the request object. They are also accessible directly on the [[PageMaker]] object. The following code demonstrates both ways to access a query argument:
10 4 Elmer de Looff
11 4 Elmer de Looff
<pre><code class="python">
12 4 Elmer de Looff
def QueryTeller(self):
13 4 Elmer de Looff
  # Retrieves the 'name' argument from the request object:
14 4 Elmer de Looff
  name = self.req.vars['get'].getfirst('name')
15 4 Elmer de Looff
16 4 Elmer de Looff
  # Retrieves the 'name' argument directly from the PageMaker instance (linked to the request):
17 4 Elmer de Looff
  name = self.get.getfirst('name')
18 4 Elmer de Looff
  return name
19 4 Elmer de Looff
</code></pre>
20 4 Elmer de Looff
21 4 Elmer de Looff
Using the @getfirst@ method, you get a single string returned from the query argument mapping, or a @None@ if no such value exists. Much like a dictionary's @get@ method, you can provide a second argument to the method, and have that returned instead as the default. 
22 4 Elmer de Looff
23 4 Elmer de Looff
Now, HTTP allows the client to provide the same query argument multiple times. Using @getfirst@ you would only get the very first defined argument. So a request that looks like @http://example.org/group?name=Bob&name=Mark&name=Jenny@ would only return 'Bob' in the previous example. To get all their names printed, you can use the following:
24 4 Elmer de Looff
25 4 Elmer de Looff
<pre><code class="python">
26 4 Elmer de Looff
def QueryTeller(self):
27 4 Elmer de Looff
  names = self.get.getlist('name')
28 4 Elmer de Looff
  return ', '.join(names)
29 4 Elmer de Looff
</code></pre>
30 4 Elmer de Looff
31 4 Elmer de Looff
This returns a neat comma-separated string with all the provided names. The @getlist@ method does not take a default, but will instead return an empty list when there are no values for the requested argument name.
32 1 Jan Klopper
33 1 Jan Klopper
h2. Post vars
34 1 Jan Klopper
35 1 Jan Klopper
POST arguments can be found under: self.post and should be accessed by issueing a 'getfirst()', or 'getlist()' call for the desired key.
36 1 Jan Klopper
self.post.getfirst() allows for a second argument to set a default if the desired key isn't set by the browser
37 1 Jan Klopper
38 1 Jan Klopper
<pre>
39 1 Jan Klopper
<code class="html">
40 1 Jan Klopper
argA = self.post.getfirst('argA')
41 1 Jan Klopper
argB = self.post.getfirst('argB', 'empty')
42 1 Jan Klopper
43 1 Jan Klopper
if 'argA' in self.post:
44 1 Jan Klopper
  #pass, argA has been send by the browser
45 1 Jan Klopper
46 1 Jan Klopper
</code>
47 1 Jan Klopper
</pre>
48 1 Jan Klopper
49 1 Jan Klopper
h2. Get vars
50 1 Jan Klopper
51 1 Jan Klopper
GET arguments can be found under: self.get, they work the same as POST vars.
52 1 Jan Klopper
53 1 Jan Klopper
h2. Cookies
54 2 Elmer de Looff
55 1 Jan Klopper
self.cookies contains the cookies send by the browser, as the interface to create them from the server.
56 1 Jan Klopper
57 1 Jan Klopper
h3. Retrieving a cookie
58 1 Jan Klopper
You can fetch the content of cookie by accessig the self.cookie dict with the name of the desired cookie as its key.
59 1 Jan Klopper
The returned cookie object has a value member containing the actual value of the requested cookie.
60 1 Jan Klopper
<pre>
61 1 Jan Klopper
<code class="python">
62 1 Jan Klopper
self.cookies['sample'].value
63 1 Jan Klopper
</code>
64 1 Jan Klopper
</pre>
65 1 Jan Klopper
66 1 Jan Klopper
h2. ENV
67 1 Jan Klopper
68 1 Jan Klopper
The env variable is a dictionary containing the following items;
69 1 Jan Klopper
* CONTENT_TYPE
70 1 Jan Klopper
* CONTENT_LENGTH
71 1 Jan Klopper
* HTTP_COOKIE
72 1 Jan Klopper
* HTTP_HOST
73 1 Jan Klopper
* HTTP_REFERER
74 1 Jan Klopper
* HTTP_USER_AGENT
75 1 Jan Klopper
* PATH_INFO
76 1 Jan Klopper
* QUERY_STRING
77 1 Jan Klopper
* REMOTE_ADDR
78 1 Jan Klopper
* REQUEST_METHOD
79 1 Jan Klopper
* UWEB_MODE 'STANDALONE' / 'MOD_PYTHON'
80 1 Jan Klopper
81 1 Jan Klopper
h3. Extended env
82 1 Jan Klopper
If more detail is required about the environment, you can issue a call to the self.req.ExtendedEnvironment() method, which will inject more details into the env var. This is a much slower operation than the normal env call, so that's why its tucked away in a separate method.
83 1 Jan Klopper
84 1 Jan Klopper
* AUTH_TYPE
85 1 Jan Klopper
* CONNECTION_ID
86 1 Jan Klopper
* DOCUMENT_ROOT
87 1 Jan Klopper
* RAW_REQUEST
88 1 Jan Klopper
* REMOTE_HOST
89 1 Jan Klopper
* REMOTE_USER
90 1 Jan Klopper
* SERVER_NAME
91 1 Jan Klopper
* SERVER_PORT
92 1 Jan Klopper
* SERVER_LOCAL_NAME
93 1 Jan Klopper
* SERVER_LOCAL_IP
94 1 Jan Klopper
* SERVER_PROTOCOL
95 1 Jan Klopper
96 2 Elmer de Looff
And in case of a @mod_python@ setup you will also get:
97 1 Jan Klopper
* MODPYTHON_HANDLER
98 1 Jan Klopper
* MODPYTHON_INTERPRETER
99 1 Jan Klopper
* MODPYTHON_PHASE