如何解析在webapp2的一个角POST请求 [英] How to parse an Angular POST request in WebApp2

查看:193
本文介绍了如何解析在webapp2的一个角POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在谷歌应用程序引擎webapp2的我角度POST请求获取数据? self.request.body 返回一个字符串,而 self.request.get(键)返回任何内容。

How do I 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.

该角code,它提交POST是:

The Angular code that submits the POST is:

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

然后这两条线在我的webapp2的处理程序:

Then these two lines in my WebApp2 handler:

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

打印:

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

什么是最好的方式从POST身体得到的数据?或者我应该不同的发送请求?

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

推荐答案

从你第一次印刷来看,似乎角发送JSON格式的数据。 webapp2的不会为你解析这些数据。为了您的特殊要求,你可以这样做:

Judging from your first print, it seems that Angular is sending the data in JSON format. Webapp2 will not parse this data for you. For your particular request, you can do:

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

如果您希望能够使用 self.request.POST.get(键)来访问POST数据,你可能需要提交数据,表单数据。请参见这个SO回答了解有关的更多信息。

If you want to be able to access the POST data using self.request.POST.get(key), you probably need to submit the data as form data. See this SO answer for more information about that.

这篇关于如何解析在webapp2的一个角POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆