【问题标题】:How do parse a curl PUT request in Flask-RESTful?如何在 Flask-RESTful 中解析 curl PUT 请求?
【发布时间】:2017-10-08 11:09:10
【问题描述】:

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

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】:

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

    我不确定是否需要通过一个 restful API 来处理这个问题,我很确定这就是 curl 导致问题的地方,也许这必须通过 flask-restful 完成的假设阻碍了你?

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      相关资源
      最近更新 更多