如何在Flask-RESTful中解析curl PUT请求?

问题描述 投票:1回答:1

如何在Flask-RESTful PUT处理程序方法中使用像curl localhost:5000/upload/test.bin --upload-file tmp/test.bin这样的curl命令保存上传的数据?

Ron Harlev's answerFlask-RESTful - Upload image的代码使用curl -F "file=@tmp/test.bin" localhost:5000/upload/test.bin的POST请求(稍微修改如下):

def post(self, filepath):
    parse = reqparse.RequestParser()
    parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
    args = parse.parse_args()
    upload_file = args['file']
    upload_file.save("/usr/tmp/{}".format(filepath))
    return ({'filepath': filepath}, 200)

但是,如果我尝试使用代码来处理来自curl --upload-file的PUT请求(当然,将post更改为put),我得到:“'NoneType'对象没有属性'save'”。这是指上面代码中的倒数第二行。

如何获取使用curl --upload-file上传的文件数据的句柄,以便将其保存到本地文件?

更新:这解决了问题:curl --request PUT -F "file=@tmp/test.bin" localhost:5000/upload/test.bin,但我仍然没有回答我的问题。

python curl flask put flask-restful
1个回答
0
投票

curls docs将--upload-file定义为PUT http请求https://curl.haxx.se/docs/manpage.html#-T

我不确定是否需要通过一个安静的API处理这个问题,而且我很确定curl会导致问题,也许这个假设必须通过烧瓶来实现,这会让你退缩吗?

也许尝试将其构建为香草烧瓶端点,此代码应该适合您。

from flask import Flask, request, jsonify
...

@app.route('/simpleupload/<string:filepath>', methods=['POST','PUT'])
def flask_upload(filepath):
    with open("/tmp/{}".format(filepath), 'wb') as file:
        file.write(request.stream.read()) 
    return (jsonify({'filepath': filepath}), 200)
© www.soinside.com 2019 - 2024. All rights reserved.