简体   繁体   English

iOS 400错误要求Flask + Zappa

[英]iOS 400 Bad Request for Flask + Zappa

I'm deploying a simple Flask app with Zappa to AWS Lambda but am running into issues. 我正在将带有Zappa的简单Flask应用程序部署到AWS Lambda,但遇到了问题。

I am sending a POST request to https://aws-ip-lambda-stuff.com/prod/chats/store which triggers: 我正在将POST请求发送到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)

If I use Postman I am able to get the request working, however using the native iOS Swift requests library it is giving a 400 BAD REQUEST error that I have not been able to sort at all. 如果我使用Postman,则可以使请求正常运行,但是使用本机iOS Swift请求库,则会出现400 BAD REQUEST错误,而我根本无法进行排序。

Does it have to do with Lambda, iOS, Zappa? 它与Lambda,iOS,Zappa有关吗? Flask? 烧瓶? Anyone got any ideas at all? 有人有任何想法吗?

Thanks 谢谢

These 400 errors usually mean that you're not sending data in your form which flask is expecting. 这400个错误通常表示您未按照烧瓶所期望的形式发送数据。 If you send in an empty form, the following line will cause the 400 error, because 'username' is not in the submitted form, but is still expected in the form. 如果您以空格式发送,则以下行将导致400错误,因为'username'不在提交的格式中,但仍应在该格式中使用。

if request.form['username'] is not None:

If you want to check if a parameter was provided in a form, use this instead: 如果要检查是否以表格形式提供了参数,请改用此方法:

if 'username' in request.form:
    do_stuff()
else:
    abort(401)

It's like trying to access the nonexistant 'c' key in the following dictionary {'a': 2, 'b': 3} , but instead of throwing a KeyError , it gives you the 400 Error 就像尝试访问以下字典{'a': 2, 'b': 3}不存在的'c'键一样,但是它没有抛出KeyError ,而是给您400 Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM