我通过为每个错误状态代码创建自定义错误处理程序解决了这个问题。我意识到
模拟类型
{'Content-Type':'application/x-www-form-urlencoded'}
使用时需要标题
request.form.get()
。这是因为表单中的信息在发送到服务器时是经过编码的。我现在使用
request.json.get()
因为我想发送数据
json
相反,现在需要的标题是
{'Content-Type':'application/json'}
模拟类型
。现在我的观点是这样的:
#subscribe.py
@app.route('/api/users/subscribe', methods=["GET", "POST"])
def subscribe():
if request.method == "GET":
try:
data = DB.get_subscription_list()
return Response(json.dumps({'result': data}, cls=DateTimeEncoder, sort_keys=True), mimetype='application/json', status=200)
except Exception:
return Response(json.dumps({'error': 'Could not fetch subscribers from database.'}), status=500)
elif request.method == "POST":
try:
email = request.json.get("email")
data = DB.add_email(email)
return Response(json.dumps({'result': 'Email added to database.'}, cls=DateTimeEncoder, sort_keys=True), mimetype='application/json', status=201)
except Exception:
return Response(json.dumps({'error': 'Could not add to database.'}), status=500)
请注意,上述每个方法的自定义错误异常。我还创建了自定义错误处理程序,以防出现带有状态代码的错误,并且我还没有为它们明确定义异常。例如,如果我得到一个状态代码为500的错误
html
,下面的错误处理程序将在中显示自定义错误
json
。我已经为状态码405、401等添加了类似的错误处理程序,以便在
json
。
@app.errorhandler(500)
def method_not_allowed(error):
return make_response(jsonify({'error': 'An error occurred during a request'}), 500)
类似地,我为数据库查询添加了一个例外:
#dbhelper.py
try:
#The following adds an email entry to the 'users' table.
uri = url_for('subscribe', email=email, _external=True)
query = "INSERT INTO users (email) VALUES (%s);"
with connection.cursor() as cursor:
cursor.execute(query,email)
connection.commit()
except Exception:
return Response(json.dumps({'error':'Database connection error.'}), status=500)
现在,我
json
当我没有以json格式发送数据或在POST请求中发送无效数据时出现异常。没有
Programmingerrors
我已经更正了代码,以获得期望的结果。
一种更有条理的方法是在单独的
errors.py
文件并在必要时调用异常函数。