折腾:
【已解决】Flask中Mongo的GridFS数据如何保存为绝对路径的下载文件地址
期间,需要去:
新弄一个gridfs的api,支持get请求,需要解析其中的2个参数 file_id和file_name,其中file_name参数希望是可选的
python – flask restful: passing parameters to GET request – Stack Overflow
webargs/flaskrestful_example.py at dev · sloria/webargs
Request Parsing — Flask-RESTful 0.2.1 documentation
flask restful get multiple parameters
去写:
<code>class GridfsAPI(Resource): def get(self, fileId, fileName): log.info("fileId=%s, file_name=%s", fileId, fileName) api.add_resource(GridfsAPI, '/gridfs/<fileId>/<fileName>', endpoint='gridfs') </code>
但是不知道具体的可选参数如何写。
看到add_resource代码中:
<code>def add_resource(self, resource, *urls, **kwargs): """Adds a resource to the api. :param resource: the class name of your resource :type resource: :class:`Resource` :param urls: one or more url routes to match for the resource, standard flask routing rules apply. Any url variables will be passed to the resource method as args. :type urls: str :param endpoint: endpoint name (defaults to :meth:`Resource.__name__.lower` Can be used to reference this route in :class:`fields.Url` fields :type endpoint: str :param resource_class_args: args to be forwarded to the constructor of the resource. :type resource_class_args: tuple :param resource_class_kwargs: kwargs to be forwarded to the constructor of the resource. :type resource_class_kwargs: dict Additional keyword arguments not specified above will be passed as-is to :meth:`flask.Flask.add_url_rule`. Examples:: api.add_resource(HelloWorld, '/', '/hello') api.add_resource(Foo, '/foo', endpoint="foo") api.add_resource(FooSpecial, '/special/foo', endpoint="foo") """ </code>
说是:
这个url参数,是根据Flask的规则去定的
所以去找找Flask的
standard flask routing rules
flask routing url parameter
API — Flask Documentation (0.12)
Advanced patterns for views and routing — Explore Flask 1.0 documentation
http://exploreflask.com/en/latest/views.html#url-converters
@app.route(‘/user/<username>’)
@app.route(‘/user/id/<int:user_id>’)
string | Accepts any text without a slash (the default). |
int | Accepts integers. |
float | Like int but for floating point values. |
path | Like string but accepts slashes. |
Quickstart — Flask Documentation (0.12)
Using URL Processors — Flask Documentation (0.12)
python – Can Flask have optional URL parameters? – Stack Overflow
I don’t think it’s possible.
好像是不支持?
还是这人的Flask-restful的这种写法
<code>api.add_resource(UserAPI, '/<userId>', '/<userId>/<username>', endpoint = 'user') </code>
是我要的
【总科】
用代码:
<code>class GridfsAPI(Resource): def get(self, fileId, fileName=None): log.info("fileId=%s, file_name=%s", fileId, fileName) api.add_resource(GridfsAPI, '/gridfs/<fileId>', '/gridfs/<fileId>/<fileName>', endpoint='gridfs') </code>
是可以获得对应的参数,支持可选参数的: