Passing HTTP parameters from request url with Flask

1k Views Asked by At

Below is the code I'm using for Simple REST API. It is working fine. The REST URL is like http://127.0.0.1:8080/sample/100 But I need to pass 100 as a key value pair like http://127.0.0.1:8080/sample?runtime=100. Is this possible or any other python library that will help

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

class Analysis(Resource):
def get(self, runtime):
   result2 = result1.to_json(orient='records')
   return json.loads(result2)

api.add_resource(Analysis, '/sample/<runtime>')

if __name__ == '__main__':
   app.run(port='8080')
1

There are 1 best solutions below

0
On

Check out "The Request Object" section of the Flask Quickstart documentation here: http://flask.pocoo.org/docs/0.12/quickstart/

To access parameters submitted in the URL (?key=value) you can use the args attribute:

searchword = request.args.get('key', '')

make sure you include this in your imports:

from flask import request