Updated: Mar. 22 2026 | Created: Mar. 22 2026
Flask @app.put() Decorator
The Flask @app.put() decorator is used to create a route that only accepts HTTP PUT requests. It is a shorter and clearer alternative to using @app.route() with methods=["PUT"].
In simple terms, @app.put() tells Flask:
"When a client sends a PUT request to this URL, run this function."
This decorator is commonly used in Flask applications and APIs when updating an existing resource.
What Is Flask @app.put()?
@app.put() is a shortcut decorator provided by the Flask application object. It works like @app.route(), but it automatically limits the route to the HTTP PUT method.
Example:
from flask import Flask
app = Flask(__name__)
@app.put("/users/<int:id>")
def update_user(id):
return f"Updated user {id}"
In this example, Flask runs the update_user() function only when the client sends a PUT request to /users/<id>.
Why Use @app.put() in Flask
You can use @app.put() when you want your code to clearly show that a route is intended only for PUT requests.
It is commonly used for:
- updating an existing database record
- replacing a resource in a REST API
- handling submitted data for full updates
- making API routes easier to read
Flask @app.put() Syntax
@app.put(rule, **options)
Flask @app.put() Parameters
rule (required)
The rule parameter is the URL pattern that Flask should match.
Example:
@app.put("/profile")
def update_profile():
return "Profile updated"
You can also create dynamic routes:
@app.put("/users/<int:id>")
def update_user(id):
return f"Updated user {id}"
Key points about rule
- it must start with
/ - it can contain variables such as
<name>or<int:id> - it defines which URL path triggers the function
**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.put("/users/<int:id>", endpoint="update_user")
def edit_user(id):
return f"Updated user {id}"
strict_slashes
Controls whether /users/1 and /users/1/ are treated as different URLs.
@app.put("/profile", strict_slashes=False)
def update_profile():
return "Profile updated"
defaults
Provides default values for route variables.
@app.put("/page/", defaults={"page": 1})
@app.put("/page/<int:page>")
def update_page(page):
return f"Updated page {page}"
subdomain
Matches a specific subdomain.
@app.put("/", subdomain="api")
def api_update():
return "API update"
provide_automatic_options
Controls whether Flask automatically handles OPTIONS requests.
Flask @app.put() Examples
Example 1: Basic PUT Route
@app.put("/profile")
def update_profile():
return "Profile updated"
Route:
http://127.0.0.1:5000/profile
This route responds only to PUT requests sent to /profile.
Example 2: Updating JSON Data
from flask import Flask, request
app = Flask(__name__)
@app.put("/api/users/<int:id>")
def update_user(id):
data = request.get_json()
return {
"id": id,
"updated_data": data
}
Example JSON request body:
{
"name": "John Updated",
"email": "john.updated@example.com"
}
This pattern is common in Flask REST APIs.
Example 3: Dynamic PUT Route
@app.put("/posts/<int:id>")
def update_post(id):
return f"Updated post {id}"
URL:
http://127.0.0.1:5000/posts/10
Output:
Updated post 10
Flask @app.put() vs @app.route()
The following two routes are equivalent:
@app.put("/users/<int:id>")
def update_user(id):
return f"Updated user {id}"
@app.route("/users/<int:id>", methods=["PUT"])
def update_user(id):
return f"Updated user {id}"
The difference is that @app.put() is shorter and makes it immediately clear that the route only accepts PUT requests.
When to use @app.put()
Use @app.put() when:
- the route should only handle PUT requests
- you want clearer and more readable code
- you are building an API for updating resources
When to use @app.route()
Use @app.route() when:
- you need to allow multiple HTTP methods on the same route
- you want a single decorator style for all routes
- you need broader routing flexibility
What Happens on a GET or POST Request?
If a route is defined with @app.put(), a GET or POST request to the same URL will not be accepted.
Example:
@app.put("/profile")
def update_profile():
return "Profile 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 PUT requests.
Flask @app.put() Return Type
A function decorated with @app.put() can return any value that Flask can convert into a response object.
Common return types
1. String
@app.put("/profile")
def update_profile():
return "Updated"
2. Dictionary
@app.put("/api/status")
def status():
return {"status": "updated"}
3. Tuple
return "Updated", 200
return {"error": "Invalid input"}, 400
return "No Content", 204
4. Response object
from flask import Response
@app.put("/custom")
def custom():
return Response("Custom response", status=200)
5. Generator or iterable
def generate():
yield "Updating "
yield "complete"
@app.put("/process")
def process():
return generate()
Flask @app.put() Complete Example
from flask import Flask, request
app = Flask(__name__)
@app.put("/api/products/<int:id>")
def update_product(id):
data = request.get_json()
return {
"message": f"Product {id} updated successfully",
"data": data
}
Example JSON request body:
{
"name": "Laptop",
"price": 599.99
}
Summary
The Flask @app.put() decorator is used to register routes that only handle HTTP PUT requests. It is a convenient shortcut for @app.route(methods=["PUT"]) and makes your code easier to read.
Use @app.put() when you want to:
- update an existing resource
- handle JSON data for full updates
- build cleaner PUT-only routes
- make route methods more explicit
If your route should only accept PUT requests, @app.put() is usually the cleaner choice.