简体   繁体   English

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

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

I am using flask-apispec with webargs to define the types of a simple API. I have produced a minimal example, below, that reproduces the issue, which is that the kwargs object is empty.我正在使用带有webargsflask-apispec来定义一个简单的 API 的类型。我在下面制作了一个最小的例子,它重现了这个问题,即kwargs object 是空的。

Server code:服务器代码:

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)

Simple client script:简单的客户端脚本:

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

Why is the kwargs dict empty?为什么kwargs字典是空的? I don't see how the above is at all different from the example in the flask-apispec documentation.我看不出上面的内容与 flask-apispec 文档中的示例有何不同。

I had the same problem and suffered a little with the documentation.我遇到了同样的问题,并且在文档方面遇到了一些问题。

Referencing webargs docs (that is what I understood flask-apispec uses under the hood) I found some examples and was able to implement it.参考webargs 文档(这是我所理解flask-apispec在幕后使用的内容)我找到了一些示例并能够实现它。

The location parameter is what was missing.缺少location参数。 In your example it would be:在您的示例中,它将是:

@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

I'm using flask-apispec v0.11.1 , which installed webargs v8.1.0 on my system.我正在使用flask-apispec v0.11.1 ,它在我的系统上安装了webargs v8.1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM