The request handler instance can access the request data using its request property. This is initialized to a populated WebOb Request object by the application.
The request object provides a get() method that returns values for arguments parsed from the query and from POST data. The method takes the argument name as its first parameter. For example:
class MyHandler(webapp2.RequestHandler): def post(self): name = self.request.get('name')
By default, get() returns the empty string ('') if the requested argument is not in the request. If the parameter default_value is specified, get() returns the value of that parameter instead of the empty string if the argument is not present.
If the argument appears more than once in a request, by default get() returns the first occurrence. To get all occurrences of an argument that might appear more than once as a list (possibly empty), give get() the argument allow_multiple=True:
# <input name="name" type="text" /> name = self.request.get("name") # <input name="subscribe" type="checkbox" value="yes" /> subscribe_to_newsletter = self.request.get("subscribe", default_value="no") # <select name="favorite_foods" multiple="true">...</select> favorite_foods = self.request.get("favorite_foods", allow_multiple=True) # for food in favorite_foods: # ...
For requests with body content that is not a set of CGI parameters, such as the body of an HTTP PUT request, the request object provides the attributes body and body_file: body is the body content as a byte string and body_file provides a file-like interface to the same data:
uploaded_file = self.request.body
Query string variables are available in request.GET.
.GET is a MultiDict: it is like a dictionary but the same key can have multiple values. When you call .get(key) for a key with multiple values, the last value is returned. To get all values for a key, use .getall(key). Examples:
request = Request.blank('/test?check=a&check=b&name=Bob') # The whole MultiDict: # GET([('check', 'a'), ('check', 'b'), ('name', 'Bob')]) get_values = request.GET # The last value for a key: 'b' check_value = request.GET['check'] # All values for a key: ['a', 'b'] check_values = request.GET.getall('check') # An iterable with alll items in the MultiDict: # [('check', 'a'), ('check', 'b'), ('name', 'Bob')] request.GET.items()
The name GET is a bit misleading, but has historical reasons: request.GET is not only available when the HTTP method is GET. It is available for any request with query strings in the URI, for any HTTP method: GET, POST, PUT etc.
Variables url encoded in the body of a request (generally a POST form submitted using the application/x-www-form-urlencoded media type) are available in request.POST.
It is also a MultiDict and can be accessed in the same way as .GET. Examples:
request = Request.blank('/') request.method = 'POST' request.body = 'check=a&check=b&name=Bob' # The whole MultiDict: # POST([('check', 'a'), ('check', 'b'), ('name', 'Bob')]) post_values = request.POST # The last value for a key: 'b' check_value = request.POST['check'] # All values for a key: ['a', 'b'] check_values = request.POST.getall('check') # An iterable with alll items in the MultiDict: # [('check', 'a'), ('check', 'b'), ('name', 'Bob')] request.POST.items()
Like GET, the name POST is a somewjat misleading, but has historical reasons: they are also available when the HTTP method is PUT, and not only POST.
request.params combines the variables from GET and POST. It can be used when you don’t care where the variable comes from.
Uploaded files are available as cgi.FieldStorage (see the cgi module) instances directly in request.POST.
The parameters from the matched webapp2.Route are set as attributes of the request object. They are request.route_args, for positional arguments, and request.route_kwargs, for keyword arguments. The matched route object is available as request.route.
A reference to the active WSGI application is also set as an attribute of the request. You can access it in request.app.
The active Request instance can be accessed during a request using the function webapp2.get_request().
A simple dictionary is available in the request object to register instances that are shared during a request: it is the webapp2.Request.registry attribute.
A registry dictionary is also available in the WSGI application object, to store objects shared across requests.