[英]Google App Engine json post request body
每當我發送包含冒號“:”的字符串時,我無法從Google應用引擎應用程序上的POST請求中讀取正文
這是我的請求處理程序類:
class MessageSync(webapp.RequestHandler):
def post(self):
print self.request.body
廣告這是我的測試腳本:
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
如果我使用變量json_works請求體被打印,但如果我使用json_doest_work,我將不會得到任何響應控制台。 除非我打印整個請求對象,否則我得到:
POST /msg
Content-Length: 134
Content-Type: application/json
Host: localhost:8080
User-Agent: Python-httplib2/$Rev$
{"sux": "test"}
為什么黑客我不能得到身體? 謝謝!
在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'
}
您應該像這樣修改您的處理程序:
class MessageSync(webapp.RequestHandler):
def post(self):
print ''
print self.request.body
甚至更好
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.