Question

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?

Was it helpful?

Solution

You can try using path placeholder instead of string:

api.add_resource(File, '/<path:fname>')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top