Updated: Mar. 22 2026 | Created: Mar. 22 2026
Flask @app.post() Decorator
The Flask @app.post() decorator is used to create a route that only accepts HTTP POST requests. It is a shorter and clearer alternative to using @app.route() with methods=["POST"].
In simple terms, @app.post() tells Flask:
"When a user sends a POST request to this URL, run this function."
This decorator is commonly used when a client needs to send data to the server, such as submitting a form, creating a new record, or sending JSON to an API endpoint.
What Is Flask @app.post()?
@app.post() is a shortcut decorator provided by the Flask application object. It works like @app.route(), but it automatically limits the route to the HTTP POST method.
Example:
from flask import Flask
app = Flask(__name__)
@app.post("/submit")
def submit():
return "Form submitted"
In this example, Flask runs the submit() function only when the client sends a POST request to /submit.
Why Use @app.post() in Flask
You can use @app.post() when you want your code to clearly show that a route is intended only for POST requests.
It is commonly used for:
- submitting HTML forms
- creating new database records
- uploading data to the server
- building API endpoints that accept JSON or form data
Flask @app.post() Syntax
@app.post(rule, **options)
Flask @app.post() Parameters
rule (required)
The rule parameter is the URL pattern that Flask should match.
Example:
@app.post("/submit")
def submit():
return "Submitted"
Here, "/submit" matches requests sent to that URL.
You can also create dynamic routes:
@app.post("/user/<int:id>/update")
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.post("/submit", endpoint="form_submit")
def submit():
return "Submitted"
strict_slashes
Controls whether /submit and /submit/ are treated as different URLs.
@app.post("/submit", strict_slashes=False)
def submit():
return "Submitted"
defaults
Provides default values for route variables.
@app.post("/page/", defaults={"page": 1})
@app.post("/page/<int:page>")
def save_page(page):
return f"Saved page {page}"
subdomain
Matches a specific subdomain.
@app.post("/", subdomain="api")
def api_submit():
return "API submit"
provide_automatic_options
Controls whether Flask automatically handles OPTIONS requests.
Flask @app.post() Examples
Example 1: Basic POST Route
@app.post("/submit")
def submit():
return "Form submitted"
Route:
http://127.0.0.1:5000/submit
This route responds only to POST requests sent to /submit.
Example 2: Handling Form Submission
from flask import Flask, request
app = Flask(__name__)
@app.post("/login")
def login():
username = request.form.get("username")
return f"Welcome, {username}!"
Example form:
<form action="/login" method="post">
<input type="text" name="username">
<button type="submit">Login</button>
</form>
When the form is submitted, Flask reads the form data from request.form.
Example 3: Handling JSON Data
from flask import Flask, request
app = Flask(__name__)
@app.post("/api/users")
def create_user():
data = request.get_json()
return {"received": data}
Example JSON request body:
{
"name": "John",
"email": "john@example.com"
}
This is common in Flask API development.
Flask @app.post() vs @app.route()
The following two routes are equivalent:
@app.post("/submit")
def submit():
return "Submitted"
@app.route("/submit", methods=["POST"])
def submit():
return "Submitted"
The difference is that @app.post() is shorter and makes it immediately clear that the route only accepts POST requests.
When to use @app.post()
Use @app.post() when:
- the route should only handle POST requests
- you want cleaner and more readable code
- you want to make the route method obvious at a glance
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 across your project
- you need broader routing flexibility
What Happens on a GET Request?
If a route is defined with @app.post(), a GET request to the same URL will not be accepted.
Example:
@app.post("/submit")
def submit():
return "Submitted"
If the client sends a GET request to /submit, Flask returns:
405 Method Not Allowed
This happens because the route only allows POST requests.
Flask @app.post() Return Type
A function decorated with @app.post() can return any value that Flask can convert into a response object.
Common return types
1. String
@app.post("/submit")
def submit():
return "Success"
2. Dictionary
@app.post("/api/status")
def status():
return {"status": "ok"}
3. Tuple
return "Created", 201
return {"error": "Invalid input"}, 400
return "Saved", 200, {"X-Custom": "value"}
4. Response object
from flask import Response
@app.post("/custom")
def custom():
return Response("Custom response", status=201)
5. Generator or iterable
def generate():
yield "Processing "
yield "complete"
@app.post("/process")
def process():
return generate()
Flask @app.post() Complete Example
from flask import Flask, request
app = Flask(__name__)
@app.post("/contact")
def contact():
name = request.form.get("name")
return f"Thanks, {name}!"
Example HTML form:
<form action="/contact" method="post">
<input type="text" name="name">
<button type="submit">Send</button>
</form>
When the form is submitted, the route handles the POST request and reads the submitted data.
Summary
The Flask @app.post() decorator is used to register routes that only handle HTTP POST requests. It is a convenient shortcut for @app.route(methods=["POST"]) and makes your code easier to read.
Use @app.post() when you want to:
- submit form data
- send JSON data to the server
- create new records
- make POST-only routes more explicit
If your route should only accept POST requests, @app.post() is usually the cleaner choice.