我使用flask-apispec和webargs来定义简单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是空的?我看不出上面的例子与瓶-apispec文档中的例子有什么不同。
发布于 2022-04-19 18:49:21
我也遇到了同样的问题,我的文档也有一点问题。
引用理发师 (这就是我在幕后理解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。
https://stackoverflow.com/questions/67574137
复制相似问题