Flask-RESTPlus - How to get query arguments?

13.9k views Asked by At

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?

3

There are 3 answers

4
nikitz On BEST ANSWER

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.

0
jbasko On

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"))
0
Hemachandran V K On

We can use request.args.get() to get the arguments .Specify the name the argument name passed in the parenthesis

Example

name = request.args.get("name")

Sample code in project

from flask import Flask, jsonify, request
  
app = Flask(__name__)
  
@app.route('/', methods = ['GET'])
def home():
    if(request.method == 'GET'):
        name = request.args.get("name")
        return jsonify({'name ': name })
  
if __name__ == '__main__':
    app.run(debug = True)