google app engine - GAE Python: Webapp2 Equivalent of Flask request.data -


i had convert python flask code 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) , not isinstance(query[0], tuple): typeerror: not 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 valid non-string sequence or mapping object 

what correct webapp2 equivalent of flask request.data ?


update
not know python flask. work gae python. trying convert sample code provided in google identity toolkit (gitkit).
flask docs,

request.data

contains incoming request data string in case came mimetype flask not handle.

as per python docs,

urllib.urlencode ()

convert mapping object or sequence of two-element tuples “percent-encoded” string, suitable pass urlopen() above optional data argument. useful pass dictionary of form fields post request. resulting string series of key=value pairs separated '&' characters, both key , value quoted using quote_plus() above. when sequence of two-element tuples used query argument, first element of each tuple key , second value. value element in can sequence , in case, if optional parameter doseq evaluates true, individual key=value pairs separated '&' generated each element of value sequence key. order of parameters in encoded string match order of parameter tuples in sequence. urlparse module provides functions parse_qs() , parse_qsl() used parse query strings python data structures.

i tried urllib2.quote (self.request.body) , no python errors came. but, gitkit rejected response saying invalid. implies urllib2.quote (self.request.body) not right python webapp2 equivalent of python flask urlencode(request.data).

if you're attempting parse post request body collection of url-encoded parameters (such submitted web form), library parses these you, , can access them dict-like interface (actually multidict since key can have more 1 value):

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

if want raw data, body field gives useful string value. error you're getting urlencode() because method not take string argument, takes mapping or sequence of two-tuples. maybe 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


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -