-
-
Save wellic/e51b761c48d27709597d1e08fa9ffdf9 to your computer and use it in GitHub Desktop.
A JSON request handler for Tornado.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import tornado.web | |
class JsonHandler(BaseHandler): | |
"""Request handler where requests and responses speak JSON.""" | |
def prepare(self): | |
# Incorporate request JSON into arguments dictionary. | |
if self.request.body: | |
try: | |
json_data = json.loads(self.request.body) | |
self.request.arguments.update(json_data) | |
except ValueError: | |
message = 'Unable to parse JSON.' | |
self.send_error(400, message=message) # Bad Request | |
# Set up response dictionary. | |
self.response = dict() | |
def set_default_headers(self): | |
self.set_header('Content-Type', 'application/json') | |
def write_error(self, status_code, **kwargs): | |
if 'message' not in kwargs: | |
if status_code == 405: | |
kwargs['message'] = 'Invalid HTTP method.' | |
else: | |
kwargs['message'] = 'Unknown error.' | |
self.response = kwargs | |
self.write_json() | |
def write_json(self): | |
output = json.dumps(self.response) | |
self.write(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment