JSON Responses

JSON ResponsesQP makes setting the reponse status, content_type and
body easy, and to actually send the response one just raises
a RespondNow exception.
Here is the Gizmo(QP) Publisher method [extracted GTi-automatically]
that responds with the JSON rendering
for the python object supplied as input data.

def json_respond_now (self, data):
”’ (data:any) -> None — sets response body with
json dump of a python object, and raises RespondNow
”’
response = self.get_hit().get_response()
response.set_status(200)
response.set_content_type(‘text/json’, ‘utf-8’)
response.set_body(json.dumps(data))
self.respond_now()

See it in action:
publisher.json_respond_now(dict(a=1, b=’two’, c=’see’))