首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >烧瓶:带有关键字参数的API URL

烧瓶:带有关键字参数的API URL
EN

Stack Overflow用户
提问于 2018-09-07 09:00:32
回答 2查看 1.2K关注 0票数 0

我想使用一个URL的‘开始’和‘到’的日期,它也是可能的,只有其中一个参数。由于这一点,我需要知道通过关键字,如果只提供一个参数,如果是‘从’或‘到’日期。

如何设置URL,以便检查是否提供了其中一个参数,并将它们作为变量在相应的类中使用?

这些线程没有解决我的问题:flask restful: passing parameters to GET requestHow to pass a URL parameter using python, Flask, and the command line

代码语言:javascript
运行
复制
class price_history(Resource):
    def get(self, from_, to):
        if from_ and to:
            return 'all data'
        if from_ and not to:
            return 'data beginning at date "from_"'
        if not from_ and to:
            return 'data going to date "to"'
        if not from_ and not to:
            return 'please provide at least one date'

api.add_resource(price_history, '/price_history/from=<from_>&to=<to>')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-07 11:27:32

this thread中提供的答案对我是有效的。它允许您完全省略URL中的可选参数。

以下是调整后的代码示例:

代码语言:javascript
运行
复制
class price_history(Resource):
    def get(self, from_=None, to=None):
        if from_ and to:
            return 'all data'
        if from_ and not to:
            return 'data beginning at date "from_"'
        if not from_ and to:
            return 'data going to date "to"'
        if not from_ and not to:
            return 'please provide at least one date'

api.add_resource(price_history,
                '/price_history/from=<from_>/to=<to>',
                '/price_history/from=<from_>',
                '/price_history/to=<to>'
                )
票数 0
EN

Stack Overflow用户

发布于 2018-09-07 09:18:56

我确实认为,随着this answer的调整,你应该能够。

代码语言:javascript
运行
复制
class Foo(Resource):
    args = {
        'from_': fields.Date(required=False),
        'to': fields.Date(required=False)
    }

    @use_kwargs(args)
    def get(self, from_, to):
        if from_ and to:
            return 'all data'
        if from_ and not to:
            return 'data beginning at date "from_"'
        if not from_ and to:
            return 'data going to date "to"'
        if not from_ and not to:
            return 'please provide at least one date'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52219025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档