简体   繁体   中英

iOS 400 Bad Request for Flask + Zappa

I'm deploying a simple Flask app with Zappa to AWS Lambda but am running into issues.

I am sending a POST request to https://aws-ip-lambda-stuff.com/prod/chats/store which triggers:

@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.

Does it have to do with 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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