Rugnar Rugnar - 1 year ago 185
Python Question

flask restful: passing parameters to GET request

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


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')


Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download