Wayne Jiang
Wayne Jiang

Reputation: 36

Flask swagger api.doc() requires positional argument but actually given

I just started to code in Flask swagger backend. When I tried to extract multiple paths and the argument parameters, an error occurs, says:

TypeError: get() missing 1 required positional argument: 'q'

But actually, my "get()" function has the positional argument 'q'. I may ask why this error occurs? That could be weird since I use

request.arg.get('q')

it works good and can returns the expected query argument.

The expected API paths is:

GET 127.0.0.1:8888/book/1?q=question20

and my code is here:

from flask import Flask, request
from flask_restplus import Resource, Api

app = Flask(__name__)
api = Api(app)


@api.route("/<string:resource>/<int:rid>")
@api.doc(params={'q': 'Your query here'})
class API(Resource):
    def get(self, resource, rid, q):
        return {"resource": resource,
                "id": rid,
                "query": q
                }, 200


if __name__ == "__main__":
    app.run(host='127.0.0.1', port=8888, debug=True)

I would appreciate if someone can help me solve this problem.

Upvotes: 0

Views: 412

Answers (1)

Adrian Krupa
Adrian Krupa

Reputation: 1937

You can use api.parser() to document and capture query parameters

parser = api.parser()
parser.add_argument('q', type=str, help='Resource name', location='args')

@api.route("/<string:resource>/<int:rid>")
@api.doc(parser=parser)
class API(Resource):
    def get(self, resource, rid):

        args = parser.parse_args()
        q = args['q']

        return {"resource": resource,
                "id": rid,
                "q":q,
                }, 200

This method is marked as deprecated https://flask-restplus.readthedocs.io/en/stable/parsing.html

Upvotes: 1

Related Questions