Skip to main content
 首页 » 编程设计

python之Flask Restful 解析 POST 数据

2024年12月31日8Leo_wl

我正在使用 cURL 向 Flask Restful API 发送一个 POST 请求:

curl -X POST -H 'Content-Type: text/csv' -d @trace.csv http://localhost:5000/upload

我无法从此请求中读取此数据,或者我不知道如何读取数据。下面是我对 API 的实现:

class ForBetaAndUpload(Resource): 
    def post(self, kind='quotes'):     
        parser = reqparse.RequestParser() 
        parser.add_argument('file') 
        args = parser.parse_args()['file'] 
        print(args) #Prints: Null 
 
api.add_resource(ForBetaAndUpload, '/upload', endpoint='upload') 
 
if __name__ == "__main__": 
    app.run(debug=True) 

如何读取使用 cURL 发送的 csv 文件数据。非常感谢你的帮助。

请您参考如下方法:

默认parser.add_argument will use GET params (location='args')。要获取 POST 数据,您需要在其参数中指定 location='form':

parser.add_argument('file', location='form')