Skip to content Skip to sidebar Skip to footer

Google App Engine: Python: Webob: How To Get Post Data In Json Format?

I am building a web app on the Google App Engine platform, which uses webapp2, which uses WebOb. I would like to POST some data in JSON format, including nested arrays and dictiona

Solution 1:

(updated with jayhendren's help) You should use $.ajax and manually set contentType='application/json; charset=utf-8' instead because $.post uses default "application/x-www-form-urlencoded;" content type. Also you need to manually encode data to JSON-string with JSON.stringify:

$.ajax({url:'/addvendor', 
        type: 'post', 
        data:JSON.stringify({'vendor': {'name': 'test', 'description': 'a good company', 'tags':['foo', 'bar']}}), 
        contentType:'application/json; charset=utf-8',
        dataType: "json",
        success:function(data){console.log(data)}})

...

print json.loads(self.request.body)

Solution 2:

use json.dumps(yourdata) don't forgot to change the header Content-Type to application/json

headers = {
"Content-Type": "application/json"
}
session.post(<url>, data=json.dumps(yourdata))

Post a Comment for "Google App Engine: Python: Webob: How To Get Post Data In Json Format?"