英文:
Requesting JSON input from Postman to run API
问题
我是新手Postman。我已经构建了机器学习模型,尝试使用API来预测新数据。我正在使用以下代码:
import pandas as pd
from flask import Flask, request, jsonify
import pickle
app = Flask(__name__)
@app.route('/model_prediction', methods=['POST'])
def model_prediction():
dic_data = request.form
file_path = dic_data["file_path"]
print(file_path)
input_data = request.json
print(input_data)
prediction_data = pd.DataFrame(input_data)
prediction_data = prediction_data.T
print(prediction_data)
with open(file_path + '/decisiontree.pkl', 'rb') as f:
model = pickle.load(f)
prediction = model.predict(prediction_data)
return jsonify('preidiction is :', prediction)
if __name__ == "__main__":
app.run(debug=True)
在Postman上运行此API时,我想获得两个输入。一个是file_path
,另一个是JSON格式的数据。以下是我如何传递这些值的屏幕截图:
通过上述过程,我得到了以下错误:
我无法识别这里的错误,陷入了困境。有人能帮助我解决这个问题吗?
英文:
I am new to Postman.I have build Machine learning model and trying to predict the new data using api.I am using below code :
import pandas as pd
from flask import Flask,request,jsonify
import pickle
app = Flask(__name__)
@app.route('/model_prediction', methods = ['POST'])
def model_prediction():
dic_data = request.form
file_path= dic_data["file_path"]
print(file_path)
input_data = request.json
print(input_data)
prediction_data = pd.DataFrame(input_data)
prediction_data =prediction_data.T
print(prediction_data)
with open(file_path+'/decisiontree.pkl','rb') as f:
model = pickle.load(f)
prediction = model.predict(prediction_data)
return jsonify('preidiction is :',prediction)
if __name__ == "__main__":
app.run(debug=True)
While running this API on Postman, I want to get two input. One is file_path
and another data in json format.Below is screenshot of how I am passing the values :
with the above process I am getting the below error as :
I am not able to identify the error here and got stuck.Can someone please help me to resolve this issue.
答案1
得分: 1
您不能混合使用 request.form
和 request.json
。您可以选择使用其中一个,但不能同时使用两者。对于REST API,应该只使用 request.json
。您可以通过接受两个键来轻松实现这一点:file_path
和 data
。因此,您可以在Postman中使用“raw” body格式执行以下操作:
{
"file_path": "/path/to/the/file",
"data": [ ...所有数字在这里 ... ]
}
现在在您的代码中,您可以访问 request.json['file_path']
和 request.json['data']
来获取它们中的每一个。
尽管如此,我强烈建议不要允许用户在JSON中指定 file_path
。他们应该如何知道服务器上文件的路径呢?而且如果他们知道,这会打开一个安全风险,因为他们可以尝试读取系统中的任何文件。
英文:
You cannot mix-and-match request.form
and request.json
. You can use one or the other, but not both at the same time. For a REST API, you should only use request.json
. You can easily do this by accepting two keys: file_path
and data
. So you can do something like this in Postman using a "raw" body format:
{
"file_path": "/path/to/the/file",
"data": [ ...all the numbers here ... ]
}
Now in your code, you can access request.json['file_path']
and request.json['data']
to get each of these.
Although, I highly recommend against allowing the user to specify the file_path
in the JSON. How are they supposed to know the path of a file on the server? And if they do, this opens a security risk because they can try to read a file anywhere in the system.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论