18

I'm curious how I can take query arguments coming from the GET method in Flask-RESTPlus. I didn't managed to find an example in the documentation.

I have previously used pure flask and the way I was doing it was by calling 'request.args.get()' from the flask library. Any ideas how to achieve this in RESTPlus?

nikitz
  • 843
  • 1
  • 10
  • 29

2 Answers2

14

It's a Flask plugin, it shouldn't be breaking the Flask interface. So you should be able to get them from flask.request as always:

import flask

...

print(flask.request.args.get("name"))
jbasko
  • 6,246
  • 1
  • 33
  • 46
9

I think the most correct solution I found is to use the request parser:

parser = api.parser()
parser.add_argument('user', location='args', help='Queried user')

It is discontinued from RESTPlus. But it is not going any time soon as they have mentioned.

CGFoX
  • 2,543
  • 3
  • 31
  • 56
nikitz
  • 843
  • 1
  • 10
  • 29
  • 8
    If it is gonna be deprecated, what is the alternative for this? PS I'm looking for something that can also be passed to `@api.expect()` to generate the Swagger doc – meshde Mar 05 '19 at 19:53
  • 2
    Hello meshde. You found a alternative? I have exactly the same problem – Neal Mc Beal Aug 19 '19 at 11:13
  • 2
    @meshde Looks like a cleaner way is to use the `param` decorator. `@api.param('flag', description='description', type='boolean')` – Vadman Nov 20 '19 at 01:47
  • @Vadman yes this is clean but how are they then accessed inside the function? I assume there should be a flask-restplus method for this; feels nasty to use flasks `request.args` – Callam Delaney Dec 30 '19 at 13:21