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

Flask @app.patch() Decorator

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

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

"When a client sends a PATCH request to this URL, run this function."

This decorator is commonly used in Flask APIs when partially updating an existing resource.

What Is Flask @app.patch()?

@app.patch() is a shortcut decorator provided by the Flask application object. It works like @app.route(), but it automatically limits the route to the HTTP PATCH method.

Example:

from flask import Flask

app = Flask(__name__)

@app.patch("/users/<int:id>")
def update_user(id):
    return f"Partially updated user {id}"

In this example, Flask runs the update_user() function only when the client sends a PATCH request to /users/<id>.

Why Use @app.patch() in Flask

You can use @app.patch() when you want your code to clearly show that a route is intended only for PATCH requests.

It is commonly used for:

Flask @app.patch() Syntax

@app.patch(rule, **options)

Flask @app.patch() Parameters

rule (required)

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

Example:

@app.patch("/profile")
def update_profile():
    return "Profile partially updated"

You can also create dynamic routes:

@app.patch("/users/<int:id>")
def update_user(id):
    return f"Partially updated user {id}"

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.patch("/users/<int:id>", endpoint="patch_user")
def edit_user(id):
    return f"Partially updated user {id}"

strict_slashes

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

@app.patch("/profile", strict_slashes=False)
def update_profile():
    return "Profile partially updated"

defaults

Provides default values for route variables.

@app.patch("/page/", defaults={"page": 1})
@app.patch("/page/<int:page>")
def update_page(page):
    return f"Partially updated page {page}"

subdomain

Matches a specific subdomain.

@app.patch("/", subdomain="api")
def api_update():
    return "API partial update"

provide_automatic_options

Controls whether Flask automatically handles OPTIONS requests.

Flask @app.patch() Examples

Example 1: Basic PATCH Route

@app.patch("/profile")
def update_profile():
    return "Profile partially updated"

Route:

http://127.0.0.1:5000/profile

This route responds only to PATCH requests sent to /profile.

Example 2: Partially Updating JSON Data

from flask import Flask, request

app = Flask(__name__)

@app.patch("/api/users/<int:id>")
def update_user(id):
    data = request.get_json()
    return {
        "id": id,
        "patched_data": data
    }

Example JSON request body:

{
  "email": "newemail@example.com"
}

This is a common pattern in Flask REST APIs when only a few fields need to be changed.

Example 3: Dynamic PATCH Route

@app.patch("/posts/<int:id>")
def update_post(id):
    return f"Partially updated post {id}"

URL:

http://127.0.0.1:5000/posts/10

Output:

Partially updated post 10

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

The following two routes are equivalent:

@app.patch("/users/<int:id>")
def update_user(id):
    return f"Partially updated user {id}"
@app.route("/users/<int:id>", methods=["PATCH"])
def update_user(id):
    return f"Partially updated user {id}"

The difference is that @app.patch() is shorter and makes it immediately clear that the route only accepts PATCH requests.

When to use @app.patch()

Use @app.patch() when:

When to use @app.route()

Use @app.route() when:

Flask PUT vs PATCH

Both PUT and PATCH are used to update resources, but they are usually used in different ways.

PUT

PUT is commonly used to replace or fully update a resource.

Example:

@app.put("/users/<int:id>")
def replace_user(id):
    return f"Fully updated user {id}"

PATCH

PATCH is commonly used to partially update a resource.

Example:

@app.patch("/users/<int:id>")
def patch_user(id):
    return f"Partially updated user {id}"

Practical difference

What Happens on a GET or POST Request?

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

Example:

@app.patch("/profile")
def update_profile():
    return "Profile partially updated"

If the client sends a GET or POST request to /profile, Flask returns:

405 Method Not Allowed

This happens because the route only allows PATCH requests.

Flask @app.patch() Return Type

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

Common return types

1. String

@app.patch("/profile")
def update_profile():
    return "Updated"

2. Dictionary

@app.patch("/api/status")
def status():
    return {"status": "patched"}

3. Tuple

return "Updated", 200
return {"error": "Invalid input"}, 400
return "", 204

4. Response object

from flask import Response

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

5. Generator or iterable

def generate():
    yield "Updating "
    yield "complete"

@app.patch("/process")
def process():
    return generate()

Flask @app.patch() Complete Example

from flask import Flask, request

app = Flask(__name__)

@app.patch("/api/products/<int:id>")
def update_product(id):
    data = request.get_json()
    return {
        "message": f"Product {id} partially updated successfully",
        "data": data
    }

Example JSON request body:

{
  "price": 499.99
}

Summary

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

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

If your route should only accept PATCH requests, @app.patch() is usually the cleaner choice.

Related Topics