Take the 2-minute tour ×

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', 
        json_works ,

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. 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!

share|improve this question
Does your actual code omit the closing single-quote in the definition of json_doesnt_work or is that just an artifact of coying the code into the question? –  Adam Crossland Feb 22 '11 at 19:53

1 Answer 1

up vote 10 down vote accepted

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!

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):
share|improve this answer
You should never use print in a request handler at all. +1 for the "even better" suggestion. –  Wooble Feb 22 '11 at 20:38
I'd upvote this answer if it didn't first suggest the awful, broken solution of using print anyway. –  Nick Johnson Feb 23 '11 at 3:26
Great answer, thank you. I later actually did try to use response.out.write and it worked, but I didn't know why print won't work. –  zigomir Feb 23 '11 at 5:48
Those print statements are outside a WSGI app - they're in CGI examples. You should never, ever use print inside a WSGI app, because the app itself is responsible for outputting the results (including headers). –  Nick Johnson Feb 23 '11 at 23:03

Your Answer


