[英]Google App Engine json post request body
I can't read body from POST request on Google app engine application whenever I send string which contains colon ":" 每当我发送包含冒号“:”的字符串时,我无法从Google应用引擎应用程序上的POST请求中读取正文
This is my request handler class: 这是我的请求处理程序类:
class MessageSync(webapp.RequestHandler):
def post(self):
print self.request.body
Ad this is my testing script: 广告这是我的测试脚本:
import httplib2
json_works = '{"works"}'
json_doesnt_work = '{"sux": "test"}'
h = httplib2.Http()
resp, content = h.request('http://localhost:8080/msg',
'POST',
json_works ,
headers={'Content-Type': 'application/json'})
print content
If I use variable json_works request body gets printed, but if I use json_doest_work I won't get any response to console. 如果我使用变量json_works请求体被打印,但如果我使用json_doest_work,我将不会得到任何响应控制台。 Except if I print whole request object I get this: 除非我打印整个请求对象,否则我得到:
POST /msg
Content-Length: 134
Content-Type: application/json
Host: localhost:8080
User-Agent: Python-httplib2/$Rev$
{"sux": "test"}
Why the hack I can't get just body? 为什么黑客我不能得到身体? Thanks! 谢谢!
In the case of json_doesnt_work
, the print
function is setting the self.request.body
as a Response header
because it's in a form of a {key:value}
parameter. 在json_doesnt_work
的情况下, print
函数将self.request.body
设置为Response header
因为它是{key:value}
参数的形式。
{'status': '200', 'content-length': '0',
'expires': 'Fri, 01 Jan 1990 00:00:00 GMT',
'server': 'Development/1.0',
'cache-control': 'no-cache',
'date': 'Tue, 22 Feb 2011 21:54:15 GMT',
'{"sux"': '"test"}', <=== HERE!
'content-type': 'text/html; charset=utf-8'
}
You should modify your handler like this: 您应该像这样修改您的处理程序:
class MessageSync(webapp.RequestHandler):
def post(self):
print ''
print self.request.body
or even better 甚至更好
class MessageSync(webapp.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = "text/plain"
self.response.out.write(self.request.body)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.