python - 发布到部署在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上时请求变量为空,我尝试向它发布。
解决方案
推荐阅读
- javascript - 导航道具是不可变的/纯粹的吗?- 反应导航 v4
- javascript - Discord JS - 将随机数组值作为命令输出的命令
- javascript - 您可以使用 Google Chrome 扩展程序更改选择文件对话框的起始目录吗?
- javascript - 从多个文本框中复制文本的 Javascript 代码,
- swift - 复杂的 Json 到 Swift Codable
- javascript - 在浏览器中使用带有 Mocha JS 的 Source Maps
- kubernetes - Helm3 发布历史仅显示最近 10 次修订
- c - 读取 /proc/pid/mem 文件不返回任何内容
- python - 使用 time.sleep() 时 Ui 对话框没有响应
- php - 如何让用户使用 PHP Sessions 登录 1 年?