How to intentionally cause a 400 Bad Request in Python/Flask?

瘦欲@ 提交于 2019-11-30 20:42:53
James Scholes

You can use abort to raise an HTTP error by status code.

from flask import abort
@app.route('/badrequest400')
def bad_request():
    abort(400)

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 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!