pythonflaskwebargsflask-apispec

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.

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? I don't see how the above is at all different from the example in the flask-apispec documentation.


Solution

  • 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.

    The location parameter is what was missing. 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.