【问题标题】:Multiple parameters to falcon service猎鹰服务的多个参数
【发布时间】:2021-10-13 21:24:30
【问题描述】:

我有一个 falcon 脚本,我试图将多个 parameters 传递给:

import falcon
import random
import os
import time


waitTime = int(os.environ.get('WAIT_TIME', '2'))

class SomeFunc(object):
    def on_get(self, req, response):
        req.path, _, query = req.relative_uri.partition('?')
        query = falcon.uri.decode(query)
        decoded_params = falcon.uri.parse_query_string(query, keep_blank=True, csv=False)
        print(decoded_params)

    ...


api = falcon.API()
api.add_route('/func', SomeFunc())

我需要将 n parameters 传递给此服务。但是,当我调用它时:

curl localhost:8000/servicedb?limit=12&time=1

只打印出第一个参数:

{'limit': '12'}

client/server 上获取所有参数的正确代码是什么?

【问题讨论】:

    标签: falconframework


    【解决方案1】:

    首先,您的 Falcon 代码看起来像您期望的那样,只是(取决于您使用的 shell)您很可能忘记转义 curl 调用,这实际上是派生一个 shell 命令 time=1 .

    另请参阅:How to include an '&' character in a bash curl statement

    发送请求

    curl "http://localhost:8000/func?limit=12&time=1"
    

    我得到以下输出:

    {'limit': '12', 'time': '1'}
    

    此外,虽然不会直接导致任何问题,但您实际上并不需要以这种方式手动解析 URI。 简单引用req.params一次性获取所有参数,或者使用专门的req.get_param_*()方法获取单个参数的值。

    例如:

    import falcon
    
    
    class SomeFunc(object):
        def on_get(self, req, response):
            print(req.params)
            print(req.get_param_as_int('limit'))
    
    
    app = falcon.App()
    app.add_route('/func', SomeFunc())
    

    现在打印:

    {'limit': '12', 'time': '1'}
    12
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      相关资源
      最近更新 更多