Flask400错误,JSON请求

2024-04-19 16:01:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了下面这个简单的flask应用程序,它显示了机器学习模型的预测值,并将索引.html直到我json.request. 然后我收到一个400错误;这是我的代码:

根据需要从命令行运行,但需要在浏览器中显示预测。有人能给我一个快速的起点吗?这是我的代码:

@app.route('/predict',methods=['POST'])
def predict():
    data = request.get_json(force=True)
    predict = [data["session"],data["time"],data["amount"]] 
    predict = np.array(predict)
    predict = predict_request.reshape(1,-1)
    #make prediction
    with graph.as_default():
        area = keras_model_loaded.predict(predict)
        output = [area[0]] 
        return render_template('index.html', output = output) 

if __name__ == "__main__":
    # Choose the port
    port = int(os.environ.get('PORT', 9000))
    # Run locally
    app.run(host='127.0.0.1', port=port)

下面是我如何生成JSON:

^{pr2}$

Tags: 代码模型机器jsonapp应用程序flaskoutput
1条回答
网友
1楼 · 发布于 2024-04-19 16:01:51

JSON数据似乎没有按预期到达服务器。请尝试将您的请求代码更新为:

post_data = {'session': session, 'time': time, 'amount': amount}

r = rq.post(url, json=post_data)

来自官方的python请求文档:

Using the json parameter in the request will change the Content-Type in the header to application/json.

相关问题 更多 >