Skip to content

Instantly share code, notes, and snippets.

@hirobert
Created January 13, 2016 20:38
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hirobert/394981a661cbf78d442e to your computer and use it in GitHub Desktop.
Save hirobert/394981a661cbf78d442e to your computer and use it in GitHub Desktop.
flask abort as json
from flask import abort, make_response, jsonify
abort(make_response(jsonify(message="Message goes here"), 400))
@stephenprn
Copy link

@pavelkomarov You can return specific status with jsonify by returning a tuple like this for example:

return jsonify({"error": "Unauthorized"}), 401

@caspii
Copy link

caspii commented Mar 13, 2023

If you have your API code in a blueprint, you can also do this I believe

@blueprint_api.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Sorry, board not found'}), 404)

The good thing here is that any other requests outside of the blueprint will continue to see the normal 404 page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment