The request handler instance builds the response using its response property.
This is initialized to an empty WebOb Response
object by the
application.
The response object’s acts as a file-like object that can be used for writing the body of the response:
class MyHandler(webapp2.RequestHandler):
def get(self):
self.response.write("<html><body><p>Hi there!</p></body></html>")
The response buffers all output in memory, then sends the final output when the handler exits. webapp2 does not support streaming data to the client.
The clear()
method erases the contents of the output buffer, leaving it
empty.
If the data written to the output stream is a Unicode value, or if the
response includes a Content-Type
header that ends with ; charset=utf-8
,
webapp2 encodes the output as UTF-8. By default, the Content-Type
header
is text/html; charset=utf-8
, including the encoding behavior. If the
Content-Type
is changed to have a different charset, webapp2 assumes the
output is a byte string to be sent verbatim.
Cookies are set in the response object. The methods to handle cookies are:
For example:
# Saves a cookie in the client.
response.set_cookie('some_key', 'value', max_age=360, path='/',
domain='example.org', secure=True)
# Deletes a cookie previously set in the client.
response.delete_cookie('bad_cookie')
# Cancels a cookie previously set in the response.
response.unset_cookie('some_key')
Only the key
parameter is required. The parameters are:
datetime
.datetime object. Use this
instead of max_age since the former is not supported by Internet Explorer.int
, e.g., request.status = 404
, or including the message,
e.g., request.status = '404 Not Found'
.int
, e.g., 404.response.headers.add(key, value)
to add keys. To get all values, use
response.headers.getall(key)
.(header_name, value)
.'text/html'
.{'charset': 'utf8'}
.response.md5_etag()
.