user1403546
user1403546

Reputation: 1749

flask restplus - pass arguments after '?' and keep arg input form in web interface

I've been able to write this code

from flask_restplus.resource import Resource
from flask_restplus import reqparse

@ns.route('')
class ExampleClass(Resource):  
    @ns.response(500, 'generic error')
    def get(self):        
        parser = reqparse.RequestParser()
        parser.add_argument('arg_name', location='args')
        args = parser.parse_args()
        print('args', args)

so that the argument 'aaa' in the request

http://localhost:8888/pathh?arg_name=aaa&

is correctly displayed from the last line of the code.

Yet the input form (in the web interface) where it's possible to insert arg_name value is of course not available anymore.

If a modify the route line to

@ns.route('/<string:arg_name>')

then the input form appears again, but the request becomes

http://localhost:8888/pathh/aaa?

and the request parser doesn't work properly anymore.

How can show a working input form in the web interface while mantaining the request in the form http://localhost:8888/pathh?arg_name=aaa& ? Thanks!

Upvotes: 2

Views: 1263

Answers (1)

HelenShy
HelenShy

Reputation: 336

Parser can be used to solve this.

parser = api.parser() parser.add_argument('param', type=int, help='Some param', location='path') # "location='path'" means look only in the querystring

You can pass parser to decorator @api.expect:

@ns.route('')
class ExampleClass(Resource):  
    @ns.response(500, 'generic error')
    @api.expect(parser)
        def get(self):        
        parser = reqparse.RequestParser()
        parser.add_argument('arg_name', location='args')
        args = parser.parse_args()
        print('args', args)

Upvotes: 1

Related Questions