Skip to main content

accept-types helps your application respond to a HTTP request in a way that a client prefers. The Accept header of an HTTP request informs the server which MIME types the client is expecting back from this request, with weighting to indicate the most prefered. If your server can respond in multiple formats (e.g.: JSON, XML, HTML), the client can easily tell your server which is the prefered format without resorting to hacks like ‘&format=json’ on the end of query strings.

Usage

get_best_match

When provided with an Accept header and a list of types your server can respond with, this function returns the clients most prefered type. This function will only return one of the acceptable types you passed in, or None if no suitable type was found:

from accept_type import get_best_match

def get_the_info(request):
        info = gather_info()

        return_type = get_best_match(request.META.get('HTTP_ACCEPT'), ['text/html', 'application/xml', 'text/json'])

        if return_type == 'application/xml':
                return render_xml(info)

        elif return_type == 'text/json':
                return render_json(info)

        elif return_type == 'text/html':
                return render_html(info)

        elif return_type == None:
                return HttpResponse406()

parse_header

When provided with an Accept header, this will parse it and return a sorted list of the clients accepted mime types. These will be instances of the AcceptableType class.

>>> from accept_type import parse_header
>>> parse_header('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
['text/html, weight 1', 'application/xhtml+xml, weight 1', 'application/xml, weight 0.9', '*/*, weight 0.8']

AcceptableType

AcceptableType instances represent one of the types that a client is willing to accept. This type could include wildcards, to match more than one MIME type.

>>> from accept_type import AcceptableType
>>> type = AcceptableType('image/*;q=0.9')
AcceptableType
>>> type.mime_type
'image/*'
>>> type.weight
0.9
>>> type.matches('image/png')
True
>>> type.matches('text/html')
False

Release files for accept-types 0.4.1

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

Source distribution (sdist)

Source distribution for accept-types 0.4.1
File Size Uploaded
accept-types-0.4.1.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for accept-types 0.4.1
File Interpreter ABI Platform
accept_types-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / accept-types-0.4.1.tar.gz

Download URL accept-types-0.4.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fb27099716d8f0360408c8ca86d69dbfed44455834b70d1506250abe521b535a
BLAKE2b-256 checksum
How to use checksums
a3846f51d94019411892c9f7fa9d461d4cef06beb35d54cd9944ea19728c4d45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

Release files / accept_types-0.4.1-py3-none-any.whl

Download URL accept_types-0.4.1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c87feccdffb66b02f9343ff387d7fd5c451ccb2e1221fbd37ea0cedef5cf290f
BLAKE2b-256 checksum
How to use checksums
0824559c0f1959134a0e7615a0ece082734209b1942365ec479cbe3bcca9098b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.0

1 release file

0.2.0

1 release file

0.1.0

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