从Postman请求JSON输入以运行API。

huangapple go评论68阅读模式
英文:

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格式的数据。以下是我如何传递这些值的屏幕截图:

从Postman请求JSON输入以运行API。

从Postman请求JSON输入以运行API。

通过上述过程,我得到了以下错误:

从Postman请求JSON输入以运行API。

我无法识别这里的错误,陷入了困境。有人能帮助我解决这个问题吗?

英文:

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 :

从Postman请求JSON输入以运行API。

从Postman请求JSON输入以运行API。

with the above process I am getting the below error as :

从Postman请求JSON输入以运行API。

I am not able to identify the error here and got stuck.Can someone please help me to resolve this issue.

答案1

得分: 1

您不能混合使用 request.formrequest.json。您可以选择使用其中一个,但不能同时使用两者。对于REST API,应该只使用 request.json。您可以通过接受两个键来轻松实现这一点:file_pathdata。因此,您可以在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.

huangapple
  • 本文由 发表于 2023年5月29日 04:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定