How to intentionally cause a 400 Bad Request in Python/Flask?
Also, You can use jsonify
from flask import jsonify
class SomeView(MethodView):
def post(self, *args, **kwargs):
if "csv_file" not in request.files:
return jsonify({'errors': 'No csv_file key in request.files.'}), 400
You can also use abort
with custom message error:
from flask import abort
abort(400, 'My custom message')
See https://flask-restplus.readthedocs.io/en/stable/errors.html
you can return the status code as a second parameter of the return
, see example below
@app.route('/my400')
def my400():
code = 400
msg = 'my message'
return msg, code
You can use abort
to raise an HTTP error by status code.
from flask import abort
@app.route('/badrequest400')
def bad_request():
abort(400)