# The Request Class - Google App Engine - Google Code

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

# The Request Class

An instance of the Request class contains information about an incoming web request.

`Request` is provided by the `google.appengine.ext.webapp` module.

The Request class inherits from the WebOb Request class. Only some of the features of the WebOb Request class are discussed here. For more information, see the WebOb documentation.

- Introduction
- Request()
- Instance methods:
  - get()
  - get_all()
  - arguments()
  - get_range()
- Instance variables inherited from WebOb Request:
  - body
  - body_file
  - remote_addr
  - url
  - path
  - query_string
  - headers
  - cookies

## Introduction

The Request class provided by webapp inherits from the WebOb Request class. webapp adds several new methods for accessing arguments submitted by web forms, and extends several default behaviors.

    import datetime

    from google.appengine.ext import webapp

    class MyRequestHandler(webapp.RequestHandler):
        def get(self):
            self.response.out.write('''
            <html>
              <body>
                <form method="post">
                  <p>Name: <input type="text" name="name" /></p>
                  <p>Favorite foods:</p>
                  <select name="favorite_foods" multiple size="4">
                    <option value="apples">Apples</option>
                    <option value="bananas">Bananas</option>
                    <option value="carrots">Carrots</option>
                    <option value="durians">Durians</option>
                  </select>
                  <p>Birth year: <input type="text" name="birth_year" /></p>
                  <p><input type="submit" /></p>
                </form>
              </body>
            </html>
            ''')

        def post(self):
            name = self.request.get("name")
            favorite_foods = self.request.get_all("favorite_foods")
            birth_year = self.request.get_range("birth_year",
                                                min_value=1900,
                                                max_value=datetime.datetime.utcnow().year,
                                                default=1900)

In addition to several new methods described below, the webapp Request class has the following differences from WebOb Request:

- If the character set is not specified in the request's Content-Type header, UTF-8 is assumed.
- Errors while decoding form arguments to Unicode are ignored. The constructor forces the following parameter to the WebOb Request constructor: `unicode_errors='ignore'`
- Form argument keys are decoded using the same character set as the values. The constructor forces the following parameter to the WebOb Request constructor: `decode_param_names=True`

## Constructor

The constructor of the Request class is defined as followed:

class Request(environ)

  An incoming request for a webapp application. Typically, the WSGIApplication instantiates a RequestHandler and initializes it with a Request object populated with a WSGI-compliant environment dictionary (environ).

  Arguments:

  environ
    A WSGI-compliant environment dictionary.

## Instance Methods

The Request class provides the following methods to instances:

get(argument_name, default_value='')

  Returns the value of the query (URL) or POST argument with the given name. If multiple arguments have the same name, the first argument's value is returned. The URL and request body are expected to be in the standard format used by web browsers for form submission.

  Arguments:

  argument_name
    The name of the argument to get.
  default_value
    The value the method should return if an argument with the given name doesn't exist. The default is the empty string.
get_all(argument_name)

  Returns a list of values of all of the query (URL) or POST arguments with the given name, possibly an empty list.

  Arguments:

  argument_name
    The name of the arguments to get.
arguments()

  Returns a list of the names of query (URL) or POST data arguments. An argument name only appears once in the list, even if the data contains multiple arguments with the same name.

get_range(name, min_value=None, max_value=None, default=0)

  Parses the query (URL) or POST data argument with the given name as an `int`, and returns it. The value is normalized to be within the given range, if any.

  Arguments:

  name
    The name of the argument to get as an integer.
  min_value
    The minimum value of the argument. If the value is less than the minimum, the method returns the minimum.
  max_value
    The maximum value of the argument. If the value is more than the maximum, the method returns the maximum.
  default
    The value to return if no argument exists with the given name.

## Instance Variables Inherited From WebOb Request

The following is a partial list of instance variable members inherited from the WebOb Request class. For more information, see the WebOb documentation.

body
  The request body, as a bytestring.
body_file
  The request body, as a StringIO instance (a file-like object).
remote_addr
  The remote user's IP address.
url
  The full request URL.
path
  The path of the URL, between the host name and the query parameters.
query_string
  The query parameters of the URL, everything after the first `?`.
headers
  The request headers, a dictionary-like object. Keys are case-insensitive.
cookies
  The cookie data from the request, a dictionary-like object.