English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# 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.

- Introduction
- Response()
- Class methods:
  - Response.http_status_message()
- Instance variables:
  - out
  - headers
- Instance methods:
  - set_status()
  - clear()
  - wsgi_write()
- Disallowed HTTP Response Headers

## 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 Disallowed HTTP Response Headers.

## 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.

## Disallowed HTTP Response Headers

For security reasons, the following HTTP response headers cannot be modified by the application. Setting these in the Response object's `headers` object has no effect.

- `Content-Encoding`
- `Content-Length`
- `Date`
- `Server`
- `Transfer-Encoding`