zig*_*mir 4 python post google-app-engine json
每当我发送包含冒号":"的字符串时,我无法从Google应用引擎应用程序上的POST请求中读取正文
这是我的请求处理程序类:
class MessageSync(webapp.RequestHandler):
def post(self):
print self.request.body
Run Code Online (Sandbox Code Playgroud)
广告这是我的测试脚本:
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
Run Code Online (Sandbox Code Playgroud)
如果我使用变量json_works请求体被打印,但如果我使用json_doest_work,我将不会得到任何响应控制台.除非我打印整个请求对象,否则我得到:
POST /msg
Content-Length: 134
Content-Type: application/json
Host: localhost:8080
User-Agent: Python-httplib2/$Rev$
{"sux": "test"}
Run Code Online (Sandbox Code Playgroud)
为什么黑客我不能得到身体?谢谢!
sys*_*out 11
在这种情况下json_doesnt_work,print函数将设置self.request.body为a,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'
}
Run Code Online (Sandbox Code Playgroud)
您应该像这样修改您的处理程序:
class MessageSync(webapp.RequestHandler):
def post(self):
print ''
print self.request.body
Run Code Online (Sandbox Code Playgroud)
甚至更好
class MessageSync(webapp.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = "text/plain"
self.response.out.write(self.request.body)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9005 次 |
| 最近记录: |