Flask RESTful中是否有一种发送.json文件和reqparse参数的方法?

时间:2019-06-10 16:37:35

标签: python-3.x flask-restful

我有一个Keras模型,希望能够通过Flask API为团队成员提供服务。此模型将.json文件作为输入,还允许对输入的某些部分进行加权,以及对部分进行加权。我希望所有查询此模型的成员都能够发送.json文件以及有关他们要加权的部分的参数。

我在Mac OS上并使用Python3。我可以通过在POST请求中仅发送.json文件并对参数进行硬编码来使其工作

curl -F 'json=@json_file.json' http://127.0.0.1:5000/model 

,并且仅在GET请求中发送权重并对json进行硬编码

curl -X GET http://127.0.0.1:5000/ -d weight_col='Birth Year' -d weights=10

但是我似乎无法让它们都作为一个请求工作。

以下是我的烧瓶应用程序代码-

app = Flask(__name__)

api = Api(app)

parser = reqparse.RequestParser()
parser.add_argument('weight_cols', action='append', location='form')
parser.add_argument('weights', action='append', location='form')

class Cluster(Resource):
    def post(self):

        req = json.load(request.files['json'])

        args = parser.parse_args()
        weight_cols = args['weight_cols']
        weights = args['weights']

        cd = ClusterData(json = req)
        cd.gen_df()
        cd.create_array(weight_cols = weight_cols, weight_amt=weights)
        output = cd.cluster_match()

        return output

api.add_resource(Cluster, '/cluster')


if __name__ == "__main__":
    app.run(debug=True)

我希望模型能够运行,但是我会收到各种错误消息,具体取决于我如何发送curl请求。如果我将其发送为

curl -F 'json=@john_davidson.json' http://127.0.0.1:5000/cluster -f weight_cols='Birth Year' -f weights=10

我收到以下错误

curl: (22) The requested URL returned error: 500 INTERNAL SERVER ERROR
curl: (6) Could not resolve host: weight_cols=Birth Year
curl: (6) Could not resolve host: weights=10

我尝试发送带有-d前缀的参数,但是同时使用-F和-d时出现错误。

我也尝试过在其中添加POST一词,但它要么告诉我暗示POST,要么给出相同的无法解析主机的信息:POST消息。

有没有办法做到这一点?在尝试此操作之前,我从未使用过Flask,但是我已经遍历了Flask RESTful的文档,但我仍然不了解如何使这项工作有效。

谢谢。

0 个答案:

没有答案