set allowed methods flask
To set the allowed methods in Flask, you can use the @app.route
decorator along with the methods
parameter. Here is how you can do it:
- Import the Flask module:
from flask import Flask
- Create an instance of the Flask class:
app = Flask(__name__)
- Define a route using the
@app.route
decorator:
@app.route('/example', methods=['GET', 'POST'])
In this example, the route is /example
and the allowed methods are GET
and POST
. You can specify any HTTP methods you want to allow, such as GET
, POST
, PUT
, DELETE
, etc.
- Define a function to handle the route:
def example():
# Your code here
This function will be executed when the route is accessed.
- Associate the route with the function:
app.add_url_rule('/example', 'example', example)
This line associates the /example
route with the example
function.
That's it! Now you have set the allowed methods for a route in Flask. You can add more routes and specify their allowed methods in a similar way. Remember to run the Flask application using app.run()
to start the server and make the routes accessible.