Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



The Response Class

An instance of the Response class represents the data to be sent in response to a web request.

Response is provided by the google.appengine.ext.webapp module.

  1. Introduction
  2. Response()
  3. Class methods:
  4. Instance variables:
  5. Instance methods:

Introduction

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 headers member.

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.

Constructor

The constructor of the Response class is defined as followed:

class Response ()

An outgoing response. Typically, the WSGIApplication instantiates a RequestHandler and initializes it with a Response object with default values.

Class Methods

The Response class provides the following class methods:

Response.http_status_message ( code )

Returns the default HTTP status message for a given HTTP status code.

Arguments

code
The HTTP status code.

Instance Variables

An instance of the Response class has the following variable members:

out

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.

headers

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 .

Instance Methods

An instance of the Response class has the following methods:

set_status ( code , message =None)

Sets the HTTP status code, and optional message for the response.

Arguments

code
The HTTP status code to use for the response.
message
The HTTP status message to use for the response. If none is given, use the default from the HTTP/1.1 specification.
clear ()

Clears all data written to the output stream ( out ).

wsgi_write ( start_response )

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.

Arguments

start_response
A WSGI-compatible start_response function.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.