Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
flask abort as json
from flask import abort, make_response, jsonify
abort(make_response(jsonify(message="Message goes here"), 400))
@tokland

This comment has been minimized.

Copy link

@tokland tokland commented Jul 7, 2017

From the docs, it wasn't obvious that flask.abort accepted also a flask.Response, very helpful, thx!

@xamgore

This comment has been minimized.

Copy link

@xamgore xamgore commented Nov 10, 2017

Also you could do just

abort(jsonify(message="Message goes here"))
@CharlyJazz

This comment has been minimized.

Copy link

@CharlyJazz CharlyJazz commented Jan 5, 2018

WWWWWWWWWWWWWWWWWWWWWOW

@tamirOK

This comment has been minimized.

Copy link

@tamirOK tamirOK commented Feb 15, 2018

@hirobert, Why do we need abort here? Can we just write return instead of abort?

@lockie

This comment has been minimized.

Copy link

@lockie lockie commented Apr 24, 2018

<3

@danigosa

This comment has been minimized.

Copy link

@danigosa danigosa commented Jul 27, 2019

Also you could do just

abort(jsonify(message="Message goes here"))

This is it 🚀

@pbuzulan

This comment has been minimized.

Copy link

@pbuzulan pbuzulan commented Aug 9, 2019

@tamirOK in decorators you want to raise errors in some cases and not to return

@radzak

This comment has been minimized.

Copy link

@radzak radzak commented Feb 3, 2020

Notice that

abort(jsonify(message="Message goes here"))

returns 200 status code.

@zhanwenchen

This comment has been minimized.

Copy link

@zhanwenchen zhanwenchen commented Mar 12, 2020

THANK YOU THIS SAVED MY LIFE.

I was sooo frustrated. My solution is this:

abort(make_response(jsonify(errors=['Your input sucks', 'our service is down', 'Google is being slow suck it']), status, HEADERS))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment