我正在使用 Zappa 将一个简单的 Flask 应用程序部署到 AWS Lambda,但遇到了问题。
我正在向 https://aws-ip-lambda-stuff.com/prod/chats/store
发送 POST 请求,该请求会触发:
@app.route('/chats/store/', methods=['POST'])
def store_chats():
if request.form['username'] is not None and request.form['password'] is not None \
and request.form['chats'] is not None:
username = request.form['username']
password = request.form['password']
chats = request.form['chats']
response = db.get_chats(username)
db.upsert_chats(username, password, chats)
if 'Item' not in response:
old_chats = ""
else:
old_chats = response['Item']['chats']
if old_chats != chats:
db.upsert_read_chats(username, False)
return jsonify({
'error': 0,
'message': 'success',
'chats_updated': old_chats != chats
})
else:
abort(401)
如果我使用 Postman,我能够使请求正常工作,但是使用 native iOS Swift 请求库,它会给出 400 BAD REQUEST
错误,我根本无法排序。
这与 Lambda、iOS、Zappa 有关吗? flask ?有人有什么想法吗?
谢谢
最佳答案
这些 400 错误通常意味着您没有以 Flask 期望的形式发送数据。如果您发送空表单,以下行将导致 400 错误,因为“用户名”不在提交的表单中,但仍应在表单中。
if request.form['username'] is not None:
如果您想检查表单中是否提供了参数,请使用以下命令:
if 'username' in request.form:
do_stuff()
else:
abort(401)
这就像尝试访问以下字典中不存在的'c'
键{'a': 2, 'b': 3}
,但不是抛出一个KeyError
,它会给你400错误
关于python - iOS 400 Flask + Zappa 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50053312/