我创建了一个简单的Flask应用程序,并且我正在读取Python的响应。
现在我的问题是,在某些条件下,我想返回一个400或500的消息响应。到目前为止,我是这样做的:
这会在终端上打印出消息。 但是在API响应中,我一直收到500错误响应。 代码的结构如下所示:
而且
当调用
response = requests.post(url,data=json.dumps(data), headers=headers )
data = json.loads(response.text)
现在我的问题是,在某些条件下,我想返回一个400或500的消息响应。到目前为止,我是这样做的:
abort(400, 'Record not found')
#or
abort(500, 'Some error...')
这会在终端上打印出消息。 但是在API响应中,我一直收到500错误响应。 代码的结构如下所示:
|--my_app
|--server.py
|--main.py
|--swagger.yml
在 server.py
文件中有以下代码:
from flask import render_template
import connexion
# Create the application instance
app = connexion.App(__name__, specification_dir="./")
# read the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")
# Create a URL route in our application for "/"
@app.route("/")
def home():
"""
This function just responds to the browser URL
localhost:5000/
:return: the rendered template "home.html"
"""
return render_template("home.html")
if __name__ == "__main__":
app.run(host="0.0.0.0", port="33")
而且
main.py
包含了我在 API 端点中使用的所有函数。
E.G:
def my_funct():
abort(400, 'Record not found')
当调用
my_funct
时,我在终端上打印出Record not found
,但在API的响应中却没有这个信息,而是始终收到500错误消息。