Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Handling Accept Headers

Posted by Armin Ronacher on 2011-02-17 @ 21:08 and filed in APIs and Microformats

If you send an HTTP request to this website it will send you some HTML back which your browser will render. However if you provide an Accept header and give application/json a higher quality than HTML or any other mimetype (or specify it as only accepted mimetype) it will send you back some JSON instead.

The Helper Function

How does this magic work? With this little helper function:

from flask import request

def request_wants_json():
    best = request.accept_mimetypes \
        .best_match(['application/json', 'text/html'])
    return best == 'application/json' and \
        request.accept_mimetypes[best] > \

Why check if json has a higher quality than HTML and not just go with the best match? Because some browsers accept on */* and we don't want to deliver JSON to an ordinary browser.


You can easily use this in your functions like this:

from flask import jsonify, render_template

def show_items():
    items = get_items_from_database()
    if request_wants_json():
        return jsonify(items=[x.to_json() for x in items])
    return render_template('show_items.html', items=items)

I recommend implementing to_json methods on your objects that return a Python object (dictionary etc.) that can be safely converted to JSON by jsonify.


To test if that works you can use the curl command line utility:

$ curl -Haccept:application/json http://localhost:5000/

This snippet by Armin Ronacher can be used freely for anything you like. Consider it public domain.