I'm building a web service that clients expect json responses even in cases of error.
I'm not familiar with CP and still struggle to learn to customize the tools/plugin.
I try to implement in the following code but it doesn't seem work.
import cherrypy
import json
def jsonify_error(*args, **kwargs):
response = cherrypy.response
response.headers['Content-Type'] = 'application/json'
response.body = json.dumps({'status': response.status, 'message': response.body})
cherrypy.tools.jsonify_error= cherrypy.Tool('after_error_response', jsonify_error, priority=20)
class Root(object):
def index(self):
return "Home page"
index.exposed = True
if __name__ == "__main__":
cp_config = { '/': {'tools.jsonify_error.on': True} }
cherrypy.quickstart(Root(), '/', config=cp_config)