flask-apispec 不使用 GET 查询中的值填充 kwargs(文档中示例代码的实现)

flask-apispec not populating kwargs with values from GET query (implementation of example code from documentation)

我使用 flask-apispecwebargs 来定义简单 API 的类型。我在下面制作了一个最小的例子,它重现了这个问题,即 kwargs 对象是空的。

服务器代码:

import flask
from flask_apispec import use_kwargs
from webargs import fields

app = flask.Flask(__name__)


@app.route('/pets', methods=["GET"])
@use_kwargs({'species': fields.Str()})
def list_pets(**kwargs):
    assert False, kwargs  # NO DATA HERE


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

简单的客户端脚本:

import requests
requests.get("http://localhost:5000/pets", params={"species": "cat"})

为什么 kwargs 字典是空的?我看不出上面的内容与 flask-apispec 文档中的示例有何不同。

我遇到了同样的问题,并且在文档方面遇到了一些问题。

引用 webargs docs(这是我所理解的 flask-apispec 在后台使用的内容)我找到了一些示例并能够实现它。

缺少 location 参数。 在您的示例中,它将是:

@app.route('/pets', methods=["GET"])
@use_kwargs({'species': fields.Str()}, location="query")
def list_pets(**kwargs):
    print(kwargs) # Now "species" should be here if sent through the query

我正在使用 flask-apispec v0.11.1,它在我的系统上安装了 webargs v8.1.0