Ehab
Ehab

Reputation: 612

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

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

Upvotes: 2

Views: 5215

Answers (1)

Jérôme
Jérôme

Reputation: 14714

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.

Upvotes: 8

Related Questions