I am developing a JSON API with Python Flask.
I want to always return JSON with an error message indicating an error occurred.
This API also accepts only JSON data in the POST body, but Flask returns an HTML 400 error by default if it cannot read the data as JSON.
Preferably, I also don’t want the user to send a header Content-Type, but if the type rawor textcontent-type, try to parse the body as JSON nonetheless.
In short, I need a way to verify that the POST body is JSON, and handle the error myself.
I read about adding a decorator to requestfor this, but not an exhaustive example.
source
share