0Pricing
Flask Academy · Lezione

Dichiarare i metodi consentiti in una route

Passi methods a @app.route per GET e POST.

Dichiarare i metodi consentiti in una route è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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! 🎉

Domande Frequenti

La lezione «Dichiarare i metodi consentiti in una route» è gratuita?

Sì — il testo completo di «Dichiarare i metodi consentiti in una route» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Dichiarare i metodi consentiti in una route»?

Passi methods a @app.route per GET e POST. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Dichiarare i metodi consentiti in una route»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Dichiarare i metodi consentiti in una route
  2. Diramare la logica con request.method
  3. Leggere i dati POST dal body
  4. Spiegazione del 405 Method Not Allowed
← Torna a Flask Academy