Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty kwargs when implementing the example code from the docs #220

Open
ifiddes opened this issue May 17, 2021 · 1 comment
Open

Empty kwargs when implementing the example code from the docs #220

ifiddes opened this issue May 17, 2021 · 1 comment

Comments

@ifiddes
Copy link

ifiddes commented May 17, 2021

I have posted this issue to StackOverflow:

https://stackoverflow.com/questions/67574137/flask-apispec-not-populating-kwargs-with-values-from-get-query-implementation-o

The following simple program below, which is nearly identical to the example provided in the docs, does not work:

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)

The kwargs field is empty.

This is with flask v1.1.2.

@amstewart
Copy link

amstewart commented Aug 22, 2021

I'm sure you've moved on since you opened this issue; but since I just hit a similar issue, I'll post the resolution.

What the above example is missing, is that the use_kwargs decoration passes through its arguments to webargs' use_args decorator. use_args needs to know what location in the HTTP request it should pull args from. By default, it will check the request's body for JSON content. The project documentation is confusing, because it is using that default behavior for what is clearly a GET method - which is debatably nonsensical. It also doesn't help that the published docs are outdated, and the locations parameter has been renamed to location.

So your example application does actually work, if you send a JSON message body in a GET request, like so...

curl --header "Content-Type: application/json" --request GET --data '{"species":"foo"}' http://localhost:5000/pets
127.0.0.1 - - [21/Aug/2021 23:25:55] "GET /pets HTTP/1.1" 500 -
[2021-08-21 23:26:34,958] ERROR in app: Exception on /pets [GET]
Traceback (most recent call last):
 ...
  File "/tmp/tmp.1QoI54ellk/./app.py", line 12, in list_pets
    assert False, kwargs  # NO DATA HERE
AssertionError: {'species': 'foo'}

But that's kind of ridiculous. I assume you want to do the more sensible thing: embedding the kwargs in the query string. To make that work, you just need to specify location='querystring' in the use_kwargs params.

@use_kwargs({'species': fields.Str()}, location='querystring')

In which case the cURL request...

curl -G 'http://localhost:5000/pets?species=foo'

Now works as you would expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants