gsinha
gsinha

Reputation: 1185

GAE Python: Webapp2 Equivalent of Flask request.data

I had to convert certain Python Flask code to Webapp2 code (used in GAE).

Flask code snippet

if request.method == 'POST':
    post_body = urlencode(request.data)

ATTEMPT 1

if self.request.method == 'POST': 
    post_body = urllib.urlencode (self.request.data)

ERROR

:: 
File "/base/data/home/apps/s~myapp/1.378592258368936474/main_v3.py", line 1397, in post
    post_body = urllib.urlencode (self.request.data)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webob-1.1.1/webob/request.py", line 1238, in __getattr__
    raise AttributeError(attr)
AttributeError: data

ATTEMPT 2

if self.request.method == 'POST': 
    post_body = urllib.urlencode (self.request.body_file)

ERROR

:: 
File "/base/data/home/apps/s~myapp/1.378591983192817348/main_v3.py", line 1397, in post
    post_body = urllib.urlencode (self.request.body_file) 
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib.py", line 1292, in urlencode
    if len(query) and not isinstance(query[0], tuple):
TypeError: not a valid non-string sequence or mapping object

ATTEMPT 3

if self.request.method == 'POST': 
    post_body = urllib.urlencode (self.request.body)

ERROR

:: 
File "/base/data/home/apps/s~myapp/1.378592109110666000/main_v3.py", line 1397, in post
  post_body = urllib.urlencode (self.request.body) 
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib.py", line 1293, in urlencode
  raise TypeError
TypeError: not a valid non-string sequence or mapping object

What is the correct Webapp2 equivalent of Flask request.data ?


UPDATE
I do not know Python Flask. I work with GAE Python. I am trying to convert the sample code provided in Google Identity Toolkit (Gitkit).
From Flask docs,

request.data

Contains the incoming request data as string in case it came with a mimetype Flask does not handle.

As per Python docs,

urllib.urlencode ()

Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string, suitable to pass to urlopen() above as the optional data argument. This is useful to pass a dictionary of form fields to a POST request. The resulting string is a series of key=value pairs separated by '&' characters, where both key and value are quoted using quote_plus() above. When a sequence of two-element tuples is used as the query argument, the first element of each tuple is a key and the second is a value. The value element in itself can be a sequence and in that case, if the optional parameter doseq is evaluates to True, individual key=value pairs separated by '&' are generated for each element of the value sequence for the key. The order of parameters in the encoded string will match the order of parameter tuples in the sequence. The urlparse module provides the functions parse_qs() and parse_qsl() which are used to parse query strings into Python data structures.

I tried urllib2.quote (self.request.body) and no Python errors came. But, Gitkit rejected the response saying it was invalid. This implies that urllib2.quote (self.request.body) is not the right Python Webapp2 equivalent of Python Flask urlencode(request.data).

Upvotes: 1

Views: 838

Answers (1)

Dan Sanderson
Dan Sanderson

Reputation: 2111

If you're attempting to parse the POST request body as a collection of URL-encoded parameters (such as what is submitted by a web form), the library parses these for you, and you can just access them with a dict-like interface (actually a MultiDict since a key can have more than one value):

field_value = self.request.POST['fieldname']

If you want the raw data, the body field gives you a useful string value. The error you're getting from urlencode() is because that method does not take a string argument, it takes a mapping or a sequence of two-tuples. Maybe this is what you're after:

post_body = urllib.urlencode(self.request.POST.items())

http://docs.webob.org/en/latest/reference.html#query-post-variables https://docs.python.org/2/library/urllib.html#urllib.urlencode

Upvotes: 1

Related Questions