Skip to content

Frequently asked questions

bdarnell edited this page Sep 3, 2012 · 3 revisions

Why doesn't RequestHandler.request.arguments interpret JSON POST bodies?

request.arguments and the methods that work with it (get_argument, get_arguments, decode_argument) are designed around the limitations and peculiarities of the x-www-form-encoded format (technically a multimap but typically used as a map, underspecified character encodings, etc). If you weren't saddled with that format to begin with, why try to fit your relatively expressive json data into its constraints?

Just do something like this, and then use self.json_args.get("foo") instead of self.get_argument("foo"):

def prepare(self):
    if self.request.headers.get("Content-Type") == "application/json":
        self.json_args = json_decode(self.request.body)

How do I make a custom 404 page?

Just add a rule at the end of your handlers list that matches everything:

(r'.*', My404Handler)

In My404Handler, use self.set_status(404) and then render a page as usual. For non-404 errors, you can override write_error in your handler class.

Clone this wiki locally