import json import logging from flask import request def verify_if_request_is_json(request): """ when a request contains invalid JSON data, this function raises a 400 error (bad request) and returns an error description. this function avoids less precise 500 Internal Server Error messages. """ if request.is_json: # when invalid json data is posted, a JSONDecodeError will be raised json.loads(request.data) return