Search code examples
pythonrestflaskflask-restful

flask restful: passing parameters to GET request


I want to create a resource that supports GET request in following way:

/bar?key1=val1&key2=val2

I tried this code, but it is not working

app = Flask(__name__)
api = Api(app)

class BarAPI(Resource):
    def get(key1, key2):
        return jsonify(dict(data=[key1, key2]))

api.add_resource(BarAPI, '/bar', endpoint='bar')

Thanks!


Solution

  • Flask can parse arguments through request

    from flask import request
    

    You can use following lines in the block that requires GET parameters. GET is declared in @app.route() declaration.

    args = request.args
    print (args) # For debugging
    no1 = args['key1']
    no2 = args['key2']
    return jsonify(dict(data=[no1, no2])) # or whatever is required