无法 return 404 错误作为 json 而不是来自 Flask-Restful 应用程序的 html
Cannot return 404 error as json instead of html from a Flask-Restful app
我正在做一个简单的 Flask REST API 测试,当我调用 {{url}}/items 时,我得到了项目列表。但是,如果调用传递到不存在的端点,例如 {{url}}/itemsss,那么我会在 html.
中收到错误 404
我想让错误处理更友好,并且 return json 而不是 html 对于某些错误,例如 400、404,405...
例如,对于 404,我试过这个:
@app.errorhandler(404)
def not_found(e):
response = jsonify({'status': 404,'error': 'not found',
'message': 'invalid resource URI'})
response.status_code = 404
return response
然而它不起作用。
我的问题与此类似:
我想知道,如果使用蓝图是完成此任务的唯一方法?
如果有更简单的方法将 404 错误输出为 json?
例如,而不是这个:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
像这样:
{
error: true,
status: 404,
code: "error.notFound",
message: "API endpoint not found",
data: { }
}
感谢您对此的帮助。
通常当我需要 return 带有 Flask-RESTful
的自定义错误消息时,我会做类似的事情:
from flask import make_response, jsonify
def custom_error(message, status_code):
return make_response(jsonify(message), status_code)
我想我在 official documentation 中找到了解决方案:
from flask import json
from werkzeug.exceptions import HTTPException
@app.errorhandler(HTTPException)
def handle_exception(e):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = e.get_response()
# replace the body with JSON
response.data = json.dumps({
"code": e.code,
"name": e.name,
"description": e.description,
})
response.content_type = "application/json"
return response
代码和描述无论如何都在 HTTP 中,只需要消息。并使用来自烧瓶的 jsonify。
from flask import jsonify
from werkzeug.exceptions import HTTPException
@app.errorhandler(HTTPException)
def handle_exception(e):
return jsonify({"message": e.description}), e.code
我正在做一个简单的 Flask REST API 测试,当我调用 {{url}}/items 时,我得到了项目列表。但是,如果调用传递到不存在的端点,例如 {{url}}/itemsss,那么我会在 html.
中收到错误 404我想让错误处理更友好,并且 return json 而不是 html 对于某些错误,例如 400、404,405...
例如,对于 404,我试过这个:
@app.errorhandler(404)
def not_found(e):
response = jsonify({'status': 404,'error': 'not found',
'message': 'invalid resource URI'})
response.status_code = 404
return response
然而它不起作用。
我的问题与此类似:
我想知道,如果使用蓝图是完成此任务的唯一方法?
如果有更简单的方法将 404 错误输出为 json?
例如,而不是这个:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
像这样:
{
error: true,
status: 404,
code: "error.notFound",
message: "API endpoint not found",
data: { }
}
感谢您对此的帮助。
通常当我需要 return 带有 Flask-RESTful
的自定义错误消息时,我会做类似的事情:
from flask import make_response, jsonify
def custom_error(message, status_code):
return make_response(jsonify(message), status_code)
我想我在 official documentation 中找到了解决方案:
from flask import json
from werkzeug.exceptions import HTTPException
@app.errorhandler(HTTPException)
def handle_exception(e):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = e.get_response()
# replace the body with JSON
response.data = json.dumps({
"code": e.code,
"name": e.name,
"description": e.description,
})
response.content_type = "application/json"
return response
代码和描述无论如何都在 HTTP 中,只需要消息。并使用来自烧瓶的 jsonify。
from flask import jsonify
from werkzeug.exceptions import HTTPException
@app.errorhandler(HTTPException)
def handle_exception(e):
return jsonify({"message": e.description}), e.code