9

您是否将应用程序配置为具有以下功能:

  1. 将 api-endpoint 定义为app.add_route('/v1/tablets', TabletsCollection())

  2. 并且仍然可以像这样使用 QueryParamshttps://example.com/api/v1/tablets?limit=12&offset=50&q=tablet name

Collection 有两种方法on_get用于检索整个列表和使用过滤器以及on_post创建单个记录。

我已经在网上搜索了一段时间,您如何获得 query_string 和正确解析的参数?

4

3 回答 3

12

查询字符串和解析的查询字符串(作为 dict)在 falcon 传递给 API 端点的 Request 对象上都可用。

您的处理程序可以执行以下操作:

class TabletsCollection():
    def on_post(self, req, resp):
        print req.query_string
        for key, value in req.params.items():
            print key, value

请参阅http://falcon.readthedocs.io/en/stable/api/request_and_response.html#Request.query_stringhttp://falcon.readthedocs.io/en/stable/api/request_and_response.html#Request.params

于 2017-10-19T22:32:24.037 回答
1

您可以使用falcon.uri模块来解析查询字符串。

qs = falcon.uri.parse_query_string(req.query_string)
        if "myq" in qs:
            searchedQ = qs["myq"]
于 2019-12-22T06:57:30.773 回答
-2

一段时间后,我发现问题并不像我一开始所想的那样与猎鹰有关。问题出在将请求代理到 gunincorn 的 Nginx 配置中。

您可以在这里使用 nginx 找到完整的答案,如何转发查询参数?或在这里简短回答:

部分 nginx 配置文件

location ~ ^/api/(.*)$ {
    # ... your code
    # need to pass all the args to proxy
    proxy_pass http://gunicorn/$1$is_args$args;
}
于 2017-10-26T09:22:46.087 回答