Declare Allowed Methods on a Route
Pass methods to @app.route for GET and POST.
Declare Allowed Methods on a Route is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Routes Default to GET
Every Flask route you write so far only answers GET requests. Browsers use GET to fetch pages, so it is the natural default.
The methods Argument
To accept other verbs, pass a methods list to the route decorator. It tells Flask exactly which HTTP verbs the view will handle.
@app.route("/submit", methods=["GET", "POST"])
def submit():
return "ok"Why List GET Explicitly
Once you set methods, the implicit GET disappears. If you still want GET, you must include it in the list yourself.
A POST-Only Route
Some endpoints should never serve a page. Make a route POST-only by listing just POST, and GET requests will be refused.
@app.route("/api/items", methods=["POST"])
def create_item():
return "created"Verbs Are Uppercase Strings
Flask expects each verb as an uppercase string like "GET" or "POST". Lowercase entries simply will not match incoming requests.
HEAD Comes for Free
When you allow GET, Flask also answers HEAD automatically. HEAD returns the same headers as GET but with no body.
OPTIONS Is Added Too
Flask auto-handles OPTIONS as well, replying with an Allow header that lists the methods your route accepts.
Beyond GET and POST
APIs often need more verbs. You can list PUT, PATCH, or DELETE the same way to support updating and removing resources.
@app.route("/api/items/<id>", methods=["PUT", "DELETE"])
def edit_item(id):
return "changed"One Function, Many Methods
A single view can accept several verbs at once. Inside it you decide what each verb does, all from one function.
Match the Verb to Intent
Use GET to read and POST to create. Choosing the right verb keeps your API predictable for every client that calls it.
Wrong Verb, No Match
If a client uses a verb you never listed, Flask refuses the request entirely. Declaring methods is your first line of access control.
Quick Check
Think about what happens to GET when you set methods.
Recap
Routes default to GET, but the methods list lets you accept POST, PUT, and more. List GET yourself if you still need it. Nicely done! 🎉
Frequently asked questions
Is the “Declare Allowed Methods on a Route” lesson free?
Yes — the full text of “Declare Allowed Methods on a Route” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declare Allowed Methods on a Route”?
Pass methods to @app.route for GET and POST. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declare Allowed Methods on a Route” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declare Allowed Methods on a Route
- Branch Logic by request.method
- Read POST Data from the Body
- 405 Method Not Allowed Explained