如何使用Flask-RESTful在REST API中传递文件路径ala Dropbox? [英] How to pass file path in a REST API ala Dropbox using Flask-RESTful?
问题描述
https://api-content.dropbox.com/1/files_put/<root>/<path>?param=val
我想用Flask-RESTful复制这个API结构。我有以下类。
$ p $ class File(restful.Resource):
def put(self ,fname):
//在这里做的东西
然后类自动映射以下代码。
app = Flask(__ name__)
api = restful.Api(app)
api.add_resource(File,'/< string:fname>')
if __name__ =='__main__':
app.run(debug = True)
使用下面的 curl
命令上传文件就可以了。
然而,以下命令失败。 这是因为 使用Flask-RESTful库来完成我想要的功能? curl 127.0.0.1:5000/foo.txt -X PUT --data-urlencode file@foo.txt $ c $
$ p
$ b
$ b $ curl 127.0.0.1: 5000 / foo / bar.txt -X PUT --data-urlencode file@bar.txt
127.0.0.1:5000/foo
被视为另一个REST资源,这个资源没有被映射到我的代码中。
path
占位符而不是 string
:
api.add_resource(File,'/< path:fname>')
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?
You can try using path
placeholder instead of string
:
api.add_resource(File, '/<path:fname>')
这篇关于如何使用Flask-RESTful在REST API中传递文件路径ala Dropbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!