Introduction
This module provides access to the Apache request data and headers the client sent with the request and the headers of the response which will be sent back. Not suprisingly, these are only valid for requests made through the mod_python interface -- pages generated by "gental" do not have this information available.
The supported interface is similar to a dictionary, where the header data can be referenced by header name (headersIn['User-Agent']) or using the "get()" method, just like with dictionaries. There is also an "add()" method which can be used to add entries to the headers.
Request Module Methods
getHeadersIn()
Arguments: None
Returns: (Header object) Headers sent from client.
Exceptions: None.
Description: This returns a header object (as described below) which includes the headers sent by the client with this request.
getHeadersOut()
Arguments: None
Returns: (Header object) Headers to be sent to the client.
Exceptions: None.
Description: This returns a header object (as described below) which includes the headers which will be sent to the client.
getRequest()
Arguments: None
Returns: (Request object).
Exceptions: None.
Description: This returns a request object including a number of methods and values which may be of use. This object is described in Section 4.5.3 of the mod_python Manual. To see some of what is contained in this object, see the "reqinfo.html" JOTWeb example, and the file "doc/mod_python-Values" in the JOTWeb source distribution.
Header Object Methods
add(name, value)
Add a header with the specified name and value to the set of headers sent to the client. This is valid only for HeadersOut.
clear(?)
As with Dictionaries?
copy()
As with Dictionaries.
get()
As with Dictionaries.
has_key()
As with Dictionaries.
items()
As with Dictionaries.
iteritems(?)
As with Dictionaries?
iterkeys(?)
As with Dictionaries?
itervalues(?)
As with Dictionaries?
keys
As with Dictionaries.
popitem()
As with Dictionaries?
setdefault(?)
As with Dictionaries.
update(?)
As with Dictionaries.
values()
As with Dictionaries.
Example
Using TAL
To gain access to the request headers, you will need to make a simple Python module that exposes the header. For example, save the following code to "headers.py":
import jotweb headersIn = jotweb.input.Headers.getHeadersIn() headersOut = jotweb.input.Headers.getHeadersOut()
Now from TAL you can display the value of the "User-Agent" header:
User-Agent: <span tal:replace="headers/headersIn/user-agent"></span>
You can make a table of the key/value pairs in TAL by using tal:repeat and accessing the "items" method of the "headersIn" dictionary:
<table>
<tr><th>Name</th><th>Value</th></tr>
<tr tal:repeat="row headers/headersIn/items">
<td tal:repeat="col row" tal:content="col">Item</td></tr>
</table>See Examples
JOTWeb includes a number of examples, one of which displays the headers of the request. See "reqinfo.html" in the "examples" directory.