I can't read body from POST request on Google app engine application whenever I send string which contains colon ":"
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 c开发者_开发技巧onsole. 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.
{'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)
精彩评论