如何使用Flask-RESTful在REST API中传递文件路径ala Dropbox? [英] How to pass file path in a REST API ala Dropbox using Flask-RESTful?

查看:368
本文介绍了如何使用Flask-RESTful在REST API中传递文件路径ala Dropbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dropbox有一个REST API,允许使用以下URL进行文件上传。 (参考



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 命令上传文件就可以了。



curl 127.0.0.1:5000/foo.txt -X PUT --data-urlencode file@foo.txt
$ 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资源,这个资源没有被映射到我的代码中。



使用Flask-RESTful库来完成我想要的功能?

可以试试 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆