Flask-RESTPlus – How to get query arguments?

Question:

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?

Asked By: nikitz

||

Answers:

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"))
Answered By: jbasko

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.

Answered By: nikitz

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)
Answered By: Hemachandran V K
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.