Skip to main content

Description

Custom Accept header routing support for Flask.

Features

Respond differently based on the MIME type accepted

Extend any given endpoint to support any additional media type.

Use custom media types to version your API

Never put a /v1/ in your URI ever again.

Dead-simple API

Yet Another Flask Decorator.

Documentation

Installation

Installing:

$ pip install flask-accept

Quickstart

Below is an example Flask app that only accepts the text/html media type:

from flask import Flask
from flask_accept import accept
app = Flask(__name__)

@app.route('/')
@accept('text/html')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

When one tries to access the endpoint without a valid Accept header:

$ curl localhost:5000 -I
HTTP/1.0 406 NOT ACCEPTABLE

With the valid header:

$ curl localhost:5000 -I -H "Accept: text/html"
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8

Adding Support for an Existing Endpoint

Given our example from before, we can add support for a different response to an additonal media type as follows:

from flask import Flask, jsonify
from flask_accept import accept
app = Flask(__name__)

@app.route('/')
@accept('text/html')
def hello_world():
    return 'Hello World!'

@hello_world.support('application/json')
def hello_world_json():
    return jsonify(result="Hello World!")

if __name__ == '__main__':
    app.run()

Now our hello_world endpoint supports JSON:

$ curl localhost:5000 -I -H "Accept: application/json"
HTTP/1.0 200 OK
Content-Type: application/json

Falling Back on a Default Endpoint

If we want to support a specific media type, but have every other request fall back to a default endpoint, we can use accept_fallback as follows:

from flask import Flask, jsonify
from flask_accept import accept, accept_fallback
app = Flask(__name__)

@app.route('/')
@accept_fallback
def hello_world():
    return 'Hello World!'

@hello_world.support('application/json')
def hello_world_json():
    return jsonify(result="Hello World!")

if __name__ == '__main__':
    app.run()

Our hello_world endpoint still supports JSON, but for any other media type (or if none is specified) it will fall back:

$ curl localhost:5000 -I
HTTP/1.0 200 OK
Content-Type: text/html

$ curl localhost:5000 -I -H "Accept: madeup/mediatype"
HTTP/1.0 200 OK
Content-Type: text/html

Use Cases

Some possible use cases for Flask-Accept.

Versioning your API

Flask-Accept let you accept any possible media type, including custom vendored media types. This is ideal for versioning an API using Accept headers only:

from flask import Flask, jsonify
from flask_accept import accept
app = Flask(__name__)

@app.route('/')
@accept('application/vnd.your_vendor.v1', 'application/vnd.your_vendor.v2')
def hello_world():
    return 'Hello World!'

@hello_world.support('application/vnd.your_vendor.v3')
def hello_world_v2():
    return 'Goodbye cruel world.'

if __name__ == '__main__':
    app.run()
$ curl localhost:5000 -H "Accept: application/vnd.your_vendor.v1"
Hello World!

$ curl localhost:5000 -H "Accept: application/vnd.your_vendor.v2"
Hello World!

$ curl localhost:5000 -H "Accept: application/vnd.your_vendor.v3"
Goodbye cruel world.

Works with Flask-RESTful Resources

The same functionality can be applied to APIs built with Flask-RESTful

from flask import Flask, jsonify
from flask_accept import accept
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)


class HelloWorldResource(Resource):
    @accept('application/vnd.your_vendor.v1', 'application/vnd.your_vendor.v2')
    def get():
        return 'Hello World!'

    @get.support('application/vnd.your_vendor.v3')
    def get_v2():
        return 'Goodbye cruel world.'


api.add_resource(HelloWorldResource, '/')

if __name__ == '__main__':
    app.run()
$ curl localhost:5000 -H "Accept: application/vnd.your_vendor.v1"
Hello World!

$ curl localhost:5000 -H "Accept: application/vnd.your_vendor.v2"
Hello World!

$ curl localhost:5000 -H "Accept: application/vnd.your_vendor.v3"
Goodbye cruel world.

Testing

To run the tests

python setup.py test

Authors

License

Open source MIT license.

Release files for flask-accept 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-accept 0.0.7
File Size Uploaded
flask_accept-0.0.7.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-accept 0.0.7
File Interpreter ABI Platform
flask_accept-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / flask_accept-0.0.7.tar.gz

Download URL flask_accept-0.0.7.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
674a49aea072b99ec20ac1e2c4b8594fc5a03aceb67b97426dd22eda5d95de24
BLAKE2b-256 checksum
How to use checksums
962fb0a3edb5dfc4e43e244140deb3308dd9f2ddf1eaf6ef24188d0ff1c64a54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2025.

Transparency log

Release files / flask_accept-0.0.7-py3-none-any.whl

Download URL flask_accept-0.0.7-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b3907594b547897e1b7bbfc150e3b122d2260b4186dd5db947cfbd74cb091f28
BLAKE2b-256 checksum
How to use checksums
b013056f3c6e46c7348ed39c864da5b657115365b0ce1db107a781f24ecbd298
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page