Google post engine

I cannot read the body from a POST request in a Google app for every time I send a line containing the colon ":"

This is the request handler class:

class MessageSync(webapp.RequestHandler): def post(self): print self.request.body 

An ad 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 the request body of the json_works variable, it will be printed, but if I use json_doest_work, I will not get any response to the console. Unless I print the entire request object, I get the following:

 POST /msg Content-Length: 134 Content-Type: application/json Host: localhost:8080 User-Agent: Python-httplib2/$Rev$ {"sux": "test"} 

Why hack I can’t get only the body? Thanks!

+4
source share
1 answer

In the case of json_doesnt_work , the print function sets self.request.body be the Response header because it is in the form of the {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 change your handler as follows:

 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) 
+11
source

Source: https://habr.com/ru/post/1340845/


All Articles