mediatype
raw JSON → 0.1.6 verified Fri May 01 auth: no python
A Python library for parsing, validating, and creating media types (MIME types) per RFC 7231. Current version 0.1.6, with monthly releases. Actively maintained.
pip install mediatype Common errors
error AttributeError: module 'mediatype' has no attribute 'MediaType' ↓
cause Importing the module instead of the class.
fix
Use: from mediatype import MediaType
error ValueError: invalid media type: 'text' ↓
cause String lacks a '/' separator.
fix
Provide a valid media type string with a slash, e.g., 'text/plain'.
Warnings
gotcha MediaType does not validate parameters keys/values beyond basic syntax. Invalid parameters may be silently accepted. ↓
fix Manually validate parameter values if strict compliance required.
gotcha Subtype suffix (e.g., +json) is stored separately; comparisons ignore suffix unless explicitly checked. ↓
fix Compare using .type and .subtype and .suffix separately or convert to string.
Imports
- MediaType
from mediatype import MediaType
Quickstart
from mediatype import MediaType
# Parse a media type string
media = MediaType('application/json; charset=utf-8')
print(media.type) # application
print(media.subtype) # json
print(media.suffix) # None
print(media.parameters) # {'charset': 'utf-8'}
# Create a media type
media2 = MediaType('application', 'json', parameters={'charset': 'utf-8'})
print(str(media2)) # application/json; charset=utf-8