flask get with parameters
from flask import Flask, request
app = Flask(__name__)
@app.route('/get_example', methods=['GET'])
def get_example():
param_value = request.args.get('param_name')
return f'The value of the parameter is: {param_value}'
if __name__ == '__main__':
app.run(debug=True)
Explanation:
- Import the necessary modules from Flask, including
Flask
andrequest
. - Create a Flask web application instance.
- Define a route '/get_example' that will be triggered when an HTTP GET request is made to this endpoint.
- Specify the allowed HTTP methods for this route as 'GET'.
- Define a function
get_example()
that will be executed when the route is accessed. - Inside the function, use
request.args.get('param_name')
to retrieve the value of the query parameter named 'param_name' from the URL. - Return a response containing the retrieved parameter value using an f-string.
- Run the Flask application with debugging enabled if the script is executed directly.