无法在PythonAnywhere上获取烧瓶python API的请求标头

问题描述 投票:0回答:1

我在PythonAnywhere上部署了一个基于flask的python应用程序。它包含一个API,该API需要以下请求标头:

app_name: <app-name>

app_key: <app-key>

下面是代码:

@app.route('/api/app/get_id', methods=['POST'])
def get_id():
    try:
        if 'app_name' and 'app_key' in request.headers:
            """
            SOME CODE
            """
        else:
            return jsonify({"status": "unauthorized", "error": "authentication parameters missing"}), 401
    except Exception as e:
        log.error("Exception occurred {}".format(e))

如您所见,它需要app_nameapp_key。如果未在标头中找到,则API只会返回错误。我从邮递员那里调用此API,其中包含app_nameapp_key

enter image description here

但是它总是抛出error authentication parameters missing。在我的本地测试中,它工作正常。我还尝试通过如下所示的日志记录进行调试:

@app.route('/api/app/get_id', methods=['POST'])
def get_id():
    try:
        log.error(request.headers)   # <-- logging
        if 'app_name' and 'app_key' in request.headers:
            """
            SOME CODE
            """
        else:
            return jsonify({"status": "unauthorized", "error": "authentication parameters missing"}), 401
    except Exception as e:
        log.error("Exception occurred {}".format(e))

但是app_nameapp_key也不存在于日志文件中。看起来由于某些原因PythonAnywhere不接受这些请求标头。有谁知道问题出在哪里,我该如何解决。请帮忙。谢谢

python flask python-requests http-headers pythonanywhere
1个回答
0
投票

['app_name' and 'app_key'在Python中的计算结果为True,因此您的if语句正在询问密钥True是否在标头中,可能不是。

尝试使用if 'app_name' in request.headers and 'app_key' in request.headers'

© www.soinside.com 2019 - 2024. All rights reserved.