最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Flask的Flask-Restful中如何获得多个GET的参数且支持可选参数

Flask crifan 9207浏览 0评论

折腾:

【已解决】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

python – flask restful: how to give multiple arguments to get methhod of any Api using flask restful? – Stack Overflow

去写:

<code>class GridfsAPI(Resource):

    def get(self, fileId, fileName):
        log.info("fileId=%s, file_name=%s", fileId, fileName)

api.add_resource(GridfsAPI, '/gridfs/&lt;fileId&gt;/&lt;fileName&gt;', 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, '/&lt;userId&gt;', '/&lt;userId&gt;/&lt;username&gt;', 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/&lt;fileId&gt;', '/gridfs/&lt;fileId&gt;/&lt;fileName&gt;', endpoint='gridfs')
</code>

是可以获得对应的参数,支持可选参数的:

转载请注明:在路上 » 【已解决】Flask的Flask-Restful中如何获得多个GET的参数且支持可选参数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.210 seconds, using 22.17MB memory