Response » History » Version 2
« Previous -
Version 2/3
(diff) -
Next » -
Current version
Elmer de Looff, 2012-04-20 17:41
TOC on the right
Response¶
HTTP requests can be answered in one of two ways in uWeb. The first, and simplest method is to simply issue a return
statement in the PageMaker and provide a string (or any object that can be reduced to a string). This will create a response to the client with a HTTP status code 200 OK, and the body of the reply will be the returned string.
def SimplePage(self):
return """<!DOCTYPE html>
<html>
<head><title>My website</title></head>
<body><h1>Hello world</h1></body>
</html>"""
This returns a minimal document to the client. Also, the default content-type will be used, and provided in the outgoing headers: text/html.
N.B.: Like all code examples on this page, this example assumes that the defined function is part of a PageMaker class, and that the uweb package itself has been imported under its default name.
If you need to change any of these, for instance setting a cookie, another content-type, or different HTTP status codes, you will need to use the Repsonse
class. The following sections will explain how to use the Response class in various situations.
Different HTTP status code¶
Returning a "Not Found" page, or even a "Internal Server Error" page is never fun, but they are two of many situations where you will want to serve content with a different HTTP status code. This is easily done using the Response
class:
def FourOhFour(self):
return uweb.Response('Unfortunately, we could not locate your document', httpcode=404)
A more verbose example, that also shows you how you would use the TemplateParser, could look like like follows. In this example, we grab the receive the requested path as argument from the Request Router, and we pass this into a template:
def FourOhFour(self, path):
return uweb.Response(self.parser('404.utp', path=path), httpcode=404)
Different content-type¶
There are also many situations where our response is not HTML. For instance, if you're developing rich applications with a lot of Javascript interaction, you will likely be answering requests using JSON. The final steps of your response would look like this:
def JsonResponse(self):
return uweb.Response(json.dumps({'name': 'µWeb'}),
content_type='application/json')
Adding custom headers¶
Adding to our previous example, if we want our JSON response to be usable cross-domain, we need to provide the correct allow-origin controls. These require a header mention, and can be added directly into the Response
object. Headers can be added by providing a dictionary with the header names and corresponding values.
def JsonGlobalResponse(self):
return uweb.Response(json.dumps({'name': 'µWeb'}),
content_type='application/json',
headers={'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache, must-revalidate'})
N.B.: Cookies should be set using the Request object's AddCookie
method, and not using the headers of the Response
object. Redirects can generally more easily be created using the Redirect
class explained below.
Redirect¶
For redirects, there is a convenience class present in uWeb. This is a subclass of Response
that only requires a new location, and optionally a HTTP status code. If the status code is not provided, it will default to 307 (Temporary Redirect):
return uweb.Redirect('http://underdark.nl')
To create a permanent redirect, simply provide the associated status code:
return uweb.Redirect('http://bugs.underdark.nl/projects/uweb/wiki/Response', httpcode=301)