How to return 400 (Bad Request) on Flask?

You have a variety of options:

The most basic:

@app.route("https://stackoverflow.com/")
def index():
    return "Record not found", 400

If you want to access the headers, you can grab the response object:

@app.route("https://stackoverflow.com/")
def index():
    resp = make_response("Record not found", 400)
    resp.headers['X-Something'] = 'A value'
    return resp

Or you can make it more explicit, and not just return a number, but return a status code object

from flask_api import status

@app.route("https://stackoverflow.com/")
def index():
    return "Record not found", status.HTTP_400_BAD_REQUEST

Further reading:

You can read more about the first two here: About Responses (Flask quickstart)
And the third here: Status codes (Flask API Guide)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)