An instance of the Response class represents the data to be sent in response to a web request.
Response is provided by the
- Class methods:
- Instance variables:
- Instance methods:
When the webapp framework calls a request handler method, the handler instance's
response member is initialized with an empty Response instance. The handler method prepares the response by manipulating the Response instance, such as by writing body data to the
out member or setting headers on the
import datetime from google.appengine.ext import webapp class MyRequestHandler(webapp.RequestHandler): def get(self): self.response.out.write("<html><body>") self.response.out.write("<p>Welcome to the Internet!</p>") self.response.out.write("</body></html>") expires_date = datetime.datetime.utcnow() + datetime.timedelta(365) expires_str = expires_date.strftime("%d %b %Y %H:%M:%S GMT") self.response.headers.add_header("Expires", expires_str)
webapp sends the response when the handler method returns. The content of the response is the final state of the Response object when the method returns.
Note: Manipulating the object in the handler method does not communicate any data to the user. In particular, this means that webapp cannot send data to the browser then perform additional logic, as in a streaming application. (App Engine applications cannot stream data to the browser, with or without webapp.)
By default, responses use a HTTP status code of 200 ("OK"). To change the status code, the application uses the set_status() method. See also the RequestHandler object's error() method for a convenient way to set error codes.
If the response does not specify a character set in the
Content-Type header, the character set for the response is set to UTF-8 automatically.
The constructor of the Response class is defined as followed:
- class Response()
The Response class provides the following class methods:
Returns the default HTTP status message for a given HTTP status code.
- The HTTP status code.
An instance of the Response class has the following variable members:
An instance of the StringIO class that contains the body text of the response. The contents of this object are sent as the body of the response when the request handler method returns.
An instance of the wsgiref.headers.Headers class that contains the headers of the response. The contents of this object are sent as the headers of the response when the request handler method returns.
For security reasons, some response headers cannot be modified by the application. See Responses.
An instance of the Response class has the following methods:
Sets the HTTP status code, and optional message for the response.
- The HTTP status code to use for the response.
- The HTTP status message to use for the response. If none is given, use the default from the HTTP/1.1 specification.
Clears all data written to the output stream (out).
Writes the response using WSGI semantics. Typically, an application does not call this directly. Instead, webapp calls this to write the response when the request handler method returns.
- A WSGI-compatible