Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Created November 10, 2017 03:40
Show Gist options
  • Save allenyang79/9d881f7c9b5909c9c3988801d4eade39 to your computer and use it in GitHub Desktop.
Save allenyang79/9d881f7c9b5909c9c3988801d4eade39 to your computer and use it in GitHub Desktop.
[flask.get_json] #flask
"""
Custom Flask and Requset.
overwrite Request.on_json_loading_failed and raise a custom exception.
"""
class CustomRequest(Request):
def on_json_loading_failed(self, e):
"""
ref: https://tedboy.github.io/flask/_modules/flask/wrappers.html#Request.on_json_loading_failed
"""
raise jsonify({'success': False, 'error_msg': 'json parse fail'}, 400)
class CustomFlask(Flask):
request_class = CustomRequest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment