views:

260

answers:

1

When using webapp from Google App Engine, how can I distinguish POST and GET? Which one gets priority and how can I seprate them? A piece of code below shows the way to obtain a POST/GET field value:

class AddWordHandler(webapp.RequestHandler):
    def post(self):
        theWord = str( self.request.get('theWord', default_value="no") )

I'd like to ask more: How to handle GET, POST and Cookies smoothly and transparently without writing your own parser?

+4  A: 

On each request, the webapp framework calls the method named after the HTTP method. Thus, GET requests call 'get()', POST requests call 'post()', and so on.

To retrieve submitted values, you can use self.request.get(), which works for both post data and query string data, or self.request.GET and self.request.POST which are multidicts for query string data and posted data, respectively.

The webapp framework's request object is based on the webob one, so for more information on this and how to handle cookies, see the webob documentation.

Nick Johnson
Hi Nick. Actually, if you followed GAE documentation, it's recommended using self.request.get() to obtain values when there is POST method in use.
Viet
@Viet: No, it's permitted - you can use it for either method - but there's absolutely no problem with using self.request.GET and self.request.POST.
Nick Johnson
Thanks Nick. I tried and it worked. I find that self.request.get() which is rather confusing.
Viet