How to parse Angular POST request in WebApp2

How to get data from my Angular POST request in Google App Engine WebApp2? self.request.body returns a string, and self.request.get(key) returns nothing.

Angular code that sends POST:

 $http.post("/mail", {request_name: 'Test Name', request_body: 'Test Body'}); 

Then these two lines in my WebApp2 handler:

 print "1: " + self.request.body print "2: " + self.request.get('request_name') 

Print this:

 1: {"request_name":"Test Name","request_body":"Test Body"} 2: 

What is the best way to get data from a POST body? Or should I send the request differently?

+7
angularjs google-app-engine webapp2
source share
3 answers

Judging by your first fingerprint, it seems that Angular is sending data in JSON format. Webapp2 will not analyze this data for you. For your specific request, you can:

 import json d = json.loads(self.request.body) v = d.get(key) 

If you want to access POST data using self.request.POST.get(key) , you probably need to send the data as form data. See this answer for more details . .

+18
source share

I always use self.request.get and can get data from the GET / POST method, maybe you are sending data in a different format, which is available only for self.request.body?

0
source share

You can try the following:

  self.request.POST.get(key) # POST requests self.request.GET.get(key) # GET requests 
0
source share

All Articles