How to pass file path in a REST API ala Dropbox using Flask-RESTful?

狂风中的少年 提交于 2019-12-21 21:29:15

问题


Dropbox has a REST API that allows file upload using the following URL. (Reference)

https://api-content.dropbox.com/1/files_put/<root>/<path>?param=val

I want to replicate this API structure using Flask-RESTful. I have the following class.

class File(restful.Resource):

    def put(self, fname):
        // do stuff here

The class is then automatically mapped with the following code.

app = Flask(__name__)
api = restful.Api(app)

api.add_resource(File, '/<string:fname>')

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

Uploading a file with the following curl command works just fine.

curl 127.0.0.1:5000/foo.txt -X PUT --data-urlencode file@foo.txt

However, the following command fails.

curl 127.0.0.1:5000/foo/bar.txt -X PUT --data-urlencode file@bar.txt

This is because 127.0.0.1:5000/foo is treated as another REST resource which isn't mapped in my code.

Is there a method of accomplishing what I want using the Flask-RESTful library?


回答1:


You can try using path placeholder instead of string:

api.add_resource(File, '/<path:fname>')


来源:https://stackoverflow.com/questions/19876612/how-to-pass-file-path-in-a-rest-api-ala-dropbox-using-flask-restful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!