Python 当应用程序通过不允许的方法(如PUT、DELETE…)请求时,如何返回json输出?

Python 当应用程序通过不允许的方法(如PUT、DELETE…)请求时,如何返回json输出?,python,api,flask,Python,Api,Flask,我正在使用Flask实现API。 问题是,当通过不允许的方法请求API时,API返回错误html页面。我想为此返回json值。 我怎么能在烧瓶里做这个 提前感谢。只需使用decorator或方法返回适当的JSON: @app.errorhandler(404) def error_404(): return jsonify(error="This resource does not exist"), 404 允许该方法,然后返回相应的响应代码和JSON。但这是有帮助的。谢谢

我正在使用Flask实现API。
问题是,当通过不允许的方法请求API时,API返回错误html页面。我想为此返回json值。 我怎么能在烧瓶里做这个

提前感谢。

只需使用decorator或方法返回适当的JSON:

@app.errorhandler(404)
def error_404():
    return jsonify(error="This resource does not exist"), 404

允许该方法,然后返回相应的响应代码和JSON。但这是有帮助的。谢谢