首页 > 解决方案 > 发布到部署在heroku上的应用程序时请求为空

问题描述

我正在尝试发布到我在 heroku 上部署的服务器,但我服务器上的“请求”变量似乎为空,因为我尝试使用 request.get_json() 导致崩溃。

我尝试使用 pytest 在本地测试服务器来测试服务器的功能,并且在执行此操作时,它运行良好。没有错误或警告。它仅在部署到 heroku 时发生。我试图发布到服务器的测试有效负载是'{“firstname”:“pelle”,“lastname”:“Johansson”,“phonenumber”:0323256327,“email”:“test@gmail.com”,“密码": "abbe123"}'

用于处理 post 请求的服务器代码 (server.py)

@app.route('/user', methods=["POST"])
def new_user():
    # Checking if request is available
    try:
        in_data = request.get_json()
    except:
        return jsonify({'msg': 'Could not get a request'}), 422

    # Checking the parameters so they contain the right data
    try:
        in_data = request.get_json()
        foo = in_data['firstname']
        foo = in_data['lastname']
        foo = in_data['phonenumber']
        foo = in_data['email']
        foo = in_data['password']
    except:
        return jsonify({'msg': 'One or more fields are missing values'}), 422

主要功能也在与上述相同的文件中(server.py)

if __name__ == '__main__':
    app.debug=True
    port1 = int(os.environ.get('PORT', 33507))
    app.run(port=port1)

我想要得到的是一个带有 json 的 200 代码返回。问题是当服务器部署在heroku上时请求变量为空,我尝试向它发布。

标签: pythonheroku

解决方案


推荐阅读