What is the difference between @app.route and @app.get in Flask?

Issue

What is the difference between these snippets?

@app.route("/greeting")
def greet():
    return "Hello World"

And

@app.get("/greeting")
def greet():
    return "Hello World"

They have the same result.

I’m familiar with @app.route() but I can’t find any documentation for @app.get

Solution

Quoted from the changelog for Flask 2.0:

Add route decorators for common HTTP methods. For example, @app.post("/login") is a shortcut for @app.route("/login", methods=["POST"]) #3907.

In your case,

@app.get("/greeting")

is equivalent to

@app.route("/greeting", methods=["GET"])

And since ["GET"] is the default, this is equivalent to

@app.route("/greeting")

Here is the documentation for @app.get.

Answered By – Jérôme

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published