Python iOS 400对Flask+;扎帕
我正在向AWS Lambda部署一个带有Zappa的简单Flask应用程序,但遇到了一些问题 我正在向Python iOS 400对Flask+;扎帕,python,ios,swift,flask,aws-lambda,Python,Ios,Swift,Flask,Aws Lambda,我正在向AWS Lambda部署一个带有Zappa的简单Flask应用程序,但遇到了一些问题 我正在向https://aws-ip-lambda-stuff.com/prod/chats/store哪些触发: @app.route('/chats/store/', methods=['POST']) def store_chats(): if request.form['username'] is not None and request.form['password'] is not
https://aws-ip-lambda-stuff.com/prod/chats/store
哪些触发:
@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,我可以让请求工作,但是使用本机iOS Swift请求库,它给出了一个400错误请求
错误,我根本无法排序
这和Lambda,iOS,Zappa有关系吗?瓶子有人有什么想法吗
谢谢这400个错误通常意味着您没有按flask所期望的格式发送数据。如果发送的表单为空,则以下行将导致400错误,因为“username”不在提交的表单中,但在表单中仍应存在
if request.form['username'] is not None:
如果要检查表单中是否提供了参数,请改用此选项:
if 'username' in request.form:
do_stuff()
else:
abort(401)
这就像试图访问以下词典中不存在的
'c'
键{'a':2,'b':3}
,但它没有抛出keyror
,而是给出了400错误
是否检查了请求中参数的编码?(swift)@qoocnguyen我该怎么做?当您将查询设置为URLRequest时,您可以通过尝试Alamofire.JSONEncoding.default.encode(您的url\u请求,with:query)
或尝试Alamofire.JSONEncoding.default.encode(url\u请求,with:query)`