# 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:
```python
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.
```python
@app.errorhandler(CustomError)
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
```python
import flask
from flask import request
from flask_json_content_type_validator import json_content_type_validator
app = flask.Flask(__name__)
@app.route('/echo-resource')
@json_content_type_validator.validator(
CustomError(message='Missing Content-Type header application/json',
error_code=1000)
)
def echo_resource():
data = dict(**request.get_json())
return flask.jsonify(data)
```
Metadata
Release files for flask_json_content_type_validator 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask_json_content_type_validator-0.1.1.tar.gz | 2.8 kB | Without provenance | Details for flask_json_content_type_validator-0.1.1.tar.gz |
Release files / flask_json_content_type_validator-0.1.1.tar.gz
| Download URL | flask_json_content_type_validator-0.1.1.tar.gz |
|---|---|
| Size | 2.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35efb998057eb8a342c212b157cd3d43a23719454f585c26a7110dad0e8da07b
|
|
BLAKE2b-256 checksum How to use checksums |
50dd9f0aa82987c0df75e1cc424ed006294e388408b6f20bac05c13c88847a39
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for flask_json_content_type_validator 0.1.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.