Hamoudaq
Hamoudaq

Reputation: 1498

flask-restful how to fetch GET parameter

i'm wondering how to fetch GET parameters in flask-restful like his /hi/city?=NY

i can do like this /hi/city/NY using /hi/city/<string:ccc> but how to do so with /hi/city?=NY .

i checked the documentation and it seems like using reqparse : http://flask-restful.readthedocs.org/en/latest/reqparse.html but still couldn't figure out how

Upvotes: 5

Views: 8423

Answers (2)

jyap
jyap

Reputation: 1594

You can use request.args.get('key')

Upvotes: 5

Phalgun
Phalgun

Reputation: 1498

You can use the RequestParser bundled along with Flask-Restful.

from flask_restful import reqparse

class YourAPI(restful.Resource):
    def get(self):

        parser = reqparse.RequestParser()
        parser.add_argument('city')
        args = parser.parse_args()

        city_value = args.get('city')  

By default, the request parser searches json, form fields and query string (as it is in your case) for the key named city. The variable city_value will have the value passed in the API request.

Upvotes: 6

Related Questions