Skip to main content

Flask JSON Content-Type decorator which validates that the Content-Type is indeed application/json. In the case of a wrong content type, then the decorator will throw an exception which can be customized.

Project description

[![Build Status](](

# Flask JSON Content Type Validation Decorator

This package contains a Flask routes decorator that validates that the request
has a Content-Type header with 'application/json'.

# Why?
Whenever you develop a Flask based API with plain vanilla flask routes that
expect to get json data, then trying to access `request.get_json()`
will return None whenever the Content-Type is not 'application/json' - which
might eventually break your code. Using this decorator will help you keep your
code clean and DRY.

# How it works?

## Create your custom exception

Below is an example of the exception you might wish to raise:

class CustomError(Exception):
def __init__(self, message, error_code):
self.message = message
self.error_code = error_code

## Register your custom error with a flask error handler

The decorator will throw your `CustomError` is the content type is not valid,
in which case you can handle and return your customized response.

def custom_error_handler(custom_error):
return flask.jsonify(dict(message=custom_error.message,
error_code=custom_error.error_code)), 400

## Decorate your route with the content type validator

import flask
from flask import request
from flask_json_content_type_validator import json_content_type_validator

app = flask.Flask(__name__)

CustomError(message='Missing Content-Type header application/json',
def echo_resource():
data = dict(**request.get_json())
return flask.jsonify(data)


Project details

Release history Release notifications

This version
History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
flask_json_content_type_validator-0.1.1.tar.gz (2.8 kB) Copy SHA256 hash SHA256 Source None Jan 4, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page