Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-RESTPlus - How to get query arguments?

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?

like image 902
nikitz Avatar asked Jan 04 '17 17:01

nikitz


People also ask

How do you pass arguments into a flask API?

How do you pass arguments in Flask API? Required Arguments To require a value be passed for an argument, just add required=True to the call to add_argument() . parser. add_argument('name', required=True, help="Name cannot be blank!")

What is Reqparse in flask RESTful?

Flask-RESTful's request parsing interface, reqparse , is modeled after the argparse interface. It's designed to provide simple and uniform access to any variable on the flask. request object in Flask.

What is namespace in flask RESTPlus?

namespace is from the Flask-RESTPlus and Blueprint is from flask to organize your app. the namespaces modules (specific to Flask-RESTPlus) are reusable namespaces designed like you would do with Flask's Blueprint.

Which of the following methods is used to embed a field inside another field?

Nested Field While nesting fields using dicts can turn a flat data object into a nested response, you can use Nested to unmarshal nested data structures and render them appropriately. This example uses two Nested fields. The Nested constructor takes a dict of fields to render as sub-fields.


2 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"))
like image 51
jbasko Avatar answered Sep 27 '22 21:09

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.

like image 35
nikitz Avatar answered Sep 27 '22 21:09

nikitz