Flask Routing Routing and HTTP methods


Example

By default, routes only respond to GET requests. You can change this behavior by supplying the methods argument to the route() decorator.

from flask import request

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        do_the_login()
    else:
        show_the_login_form()

You can also map different functions to the same endpoint based on the HTTP method used.

@app.route('/endpoint', methods=['GET'])
def get_endpoint():
    #respond to GET requests for '/endpoint'


@app.route('/endpoint', methods=['POST', 'PUT', 'DELETE'])
def post_or_put():
    #respond to POST, PUT, or DELETE requests for '/endpoint'