paulb@470 | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paulb@470 | 2 | <html xmlns="http://www.w3.org/1999/xhtml"><head> |
paulb@470 | 3 | <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> |
paulb@470 | 4 | |
paulb@470 | 5 | |
paulb@470 | 6 | <title>Request Headers</title><meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" /> |
paulb@470 | 7 | <link href="styles.css" rel="stylesheet" type="text/css" /></head> |
paulb@470 | 8 | <body> |
paulb@470 | 9 | <h1>Request Headers</h1> |
paulb@470 | 10 | |
paulb@470 | 11 | |
paulb@470 | 12 | <p>Request headers are pieces of information that describe certain aspects of a request, such as:</p> |
paulb@470 | 13 | <ul> |
paulb@470 | 14 | <li>The location to which the request is being sent.</li> |
paulb@470 | 15 | <li>The type of information in the request body.</li> |
paulb@470 | 16 | <li>Preferences about which kinds of information the sender would like to receive in return.</li> |
paulb@470 | 17 | </ul> |
paulb@470 | 18 | <p>The location and related query information is conveniently accessible through <a href="paths.html">path</a> information and <a href="parameters-headers.html">request header parameter</a> information. Other types of header information are made available through other WebStack API methods.</p> |
paulb@470 | 19 | <h2>Content Types</h2> |
paulb@470 | 20 | <p>When a Web application sends some information in a response, it |
paulb@470 | 21 | describes the content type of that information, and this is described |
paulb@470 | 22 | in the "Responses and Presentation" document. However, it is also |
paulb@470 | 23 | possible that information sent to the application is also described |
paulb@470 | 24 | using a content type, and such details may be investigated using code |
paulb@470 | 25 | similar to the following:</p> |
paulb@470 | 26 | |
paulb@470 | 27 | <pre>class MyResource:<br /> def respond(self, trans):<br /> content_type = trans.get_content_type() # returns a WebStack.Generic.ContentType object</pre> |
paulb@470 | 28 | |
paulb@470 | 29 | <p>Unfortunately, such information is not always provided by Web browsers.</p> |
paulb@470 | 30 | <h2>Content Preferences</h2> |
paulb@470 | 31 | <p>Sometimes, Web browsers describe the kinds of information that |
paulb@470 | 32 | they are willing to receive, and WebStack provides various means to |
paulb@470 | 33 | query such preferences:</p> |
paulb@470 | 34 | |
paulb@470 | 35 | <pre> languages = trans.get_content_languages() # returns a list of language codes<br /> charsets = trans.get_content_charsets() # returns a list of character set identifiers<br /></pre> |
paulb@470 | 36 | <p>This information permits us to send content which matches the |
paulb@470 | 37 | expectations of the user, or at least the expectations of the user's |
paulb@470 | 38 | software.</p> |
paulb@470 | 39 | <h2>Other Headers</h2> |
paulb@470 | 40 | <p>Various other pieces of information may be attached to the request |
paulb@470 | 41 | as headers, and such information can be accessed through the general |
paulb@470 | 42 | header access methods as described below. Each header has a particular |
paulb@470 | 43 | name which is associated with a corresponding value.</p> |
paulb@470 | 44 | <div class="WebStack"> |
paulb@470 | 45 | <h3>WebStack API - Accessing Header Information</h3> |
paulb@470 | 46 | |
paulb@470 | 47 | <p>Transaction objects provide the following methods to access request header information:</p> |
paulb@470 | 48 | <dl> |
paulb@470 | 49 | <dt><code>get_headers</code></dt> |
paulb@470 | 50 | <dd>This returns a |
paulb@470 | 51 | dictionary mapping header names to single string values.<br /> |
paulb@470 | 52 | </dd> |
paulb@470 | 53 | <dt><code>get_header_values</code></dt> |
paulb@470 | 54 | <dd>Given a header name as parameter, this method returns a list of string values associated with that name.</dd><dt><code>get_content_type</code></dt> |
paulb@470 | 55 | <dd>This returns a content type object (typically <code>WebStack.Generic.ContentType</code>) describing the incoming request body content.</dd> |
paulb@470 | 56 | <dt><code>get_content_languages</code></dt> |
paulb@470 | 57 | <dd>This returns a list of language identifiers, in descending order |
paulb@470 | 58 | of preference, indicating in which languages the sender of the request |
paulb@470 | 59 | would prefer to receive information.</dd> |
paulb@470 | 60 | <dt><code>get_content_charsets</code></dt> |
paulb@470 | 61 | <dd>This returns a list of character set identifiers, in descending |
paulb@470 | 62 | order of preference, indicating in which character sets the sender of |
paulb@470 | 63 | the request would prefer to receive information.</dd> |
paulb@470 | 64 | |
paulb@470 | 65 | </dl> |
paulb@470 | 66 | <p>It should be noted that the <code>get_headers</code> and <code>get_header_values</code> methods |
paulb@470 | 67 | present a slightly different view of the available header information, |
paulb@470 | 68 | in that only a single header value is made available through the <code>get_headers</code> method for each header name, whereas <code>get_header_values</code> provides potentially many values for the same header name. </p> |
paulb@470 | 69 | |
paulb@470 | 70 | </div></body></html> |