ComputerGeek
search
Flask @app.get() Decorator — Syntax, Usage, and Examples Updated: Mar. 22 2026 | Created: Mar. 22 2026

Flask @app.get() Decorator

The Flask @app.get() decorator is used to create a route that only accepts HTTP GET requests. It is a shorter and cleaner alternative to using @app.route() with methods=["GET"].

In simple terms, @app.get() tells Flask:

"When a user sends a GET request to this URL, run this function."

This decorator is useful when you want a route to respond only to GET requests, such as displaying a page or returning data.

What Is Flask @app.get()?

@app.get() is a shortcut decorator provided by the Flask application object. It works like @app.route(), but it automatically sets the allowed HTTP method to GET.

Example:

from flask import Flask

app = Flask(__name__)

@app.get("/")
def home():
    return "Hello, World!"

In this example, Flask runs the home() function only when the client makes a GET request to /.

Why Use @app.get() in Flask

You can use @app.get() when you want to make your code more readable and clearly show that a route is meant only for GET requests.

It is commonly used for:

Flask @app.get() Syntax

@app.get(rule, **options)

Flask @app.get() Parameters

rule (required)

The rule parameter is the URL pattern that Flask should match.

Example:

@app.get("/")
def home():
    return "Home Page"

Here, "/" matches the root URL.

You can also create dynamic routes:

@app.get("/user/<username>")
def profile(username):
    return f"User: {username}"

Key points about rule

**options (optional)

The **options parameter accepts the same route options as @app.route().

Common options include:

endpoint

Defines the internal name of the route.

@app.get("/home", endpoint="homepage")
def home():
    return "Home"

strict_slashes

Controls whether /about and /about/ are treated as different URLs.

@app.get("/about", strict_slashes=False)
def about():
    return "About page"

defaults

Provides default values for route variables.

@app.get("/page/", defaults={"page": 1})
@app.get("/page/<int:page>")
def show_page(page):
    return f"Page {page}"

subdomain

Matches a specific subdomain.

@app.get("/", subdomain="api")
def api_home():
    return "API Home"

provide_automatic_options

Controls whether Flask automatically handles OPTIONS requests.

Flask @app.get() Examples

Example 1: Basic GET Route

@app.get("/")
def hello():
    return "Hello, World!"

URL:

http://127.0.0.1:5000/

Output:

Hello, World!

Example 2: Dynamic GET Route

@app.get("/<name>")
def hello(name):
    return f"Hello, {name}!"

URL:

http://127.0.0.1:5000/John

Output:

Hello, John!

Example 3: GET Route With Type Converter

@app.get("/post/<int:id>")
def show_post(id):
    return f"Post ID: {id}"

URL:

http://127.0.0.1:5000/post/10

Output:

Post ID: 10

Flask @app.get() vs @app.route()

The following two routes are equivalent:

@app.get("/users")
def users():
    return "Users"
@app.route("/users", methods=["GET"])
def users():
    return "Users"

The difference is that @app.get() is shorter and makes the allowed method more obvious.

When to use @app.get()

Use @app.get() when:

When to use @app.route()

Use @app.route() when:

What Happens on a POST Request?

If a route is defined with @app.get(), a POST request to the same URL will not be accepted.

Example:

@app.get("/contact")
def contact():
    return "Contact page"

If the client sends a POST request to /contact, Flask returns:

405 Method Not Allowed

This happens because the route only allows GET requests.

Flask @app.get() Return Type

A function decorated with @app.get() can return any value that Flask can convert into a response object.

Common return types

1. String

@app.get("/")
def home():
    return "Hello, World!"

2. Dictionary

@app.get("/json")
def json_route():
    return {"message": "Hello"}

3. Tuple

return "Not Found", 404
return {"error": "Bad Request"}, 400
return "OK", 200, {"X-Custom": "value"}

4. Response object

from flask import Response

@app.get("/custom")
def custom():
    return Response("Custom response", status=200)

5. Generator or iterable

def generate():
    yield "Hello "
    yield "World"

@app.get("/stream")
def stream():
    return generate()

Flask @app.get() Complete Example

from flask import Flask

app = Flask(__name__)

@app.get("/")
def home():
    return "Home Page"

@app.get("/user/<name>")
def user(name):
    return f"Hello, {name}!"

Output

Summary

The Flask @app.get() decorator is used to register routes that only handle HTTP GET requests. It is a convenient shortcut for @app.route(methods=["GET"]) and makes your code easier to read.

Use @app.get() when you want to:

If you only need a route for GET requests, @app.get() is usually the cleaner choice.

Related Topics