0Pricing
Flask Academy · Lezione

Impostare codici di stato nelle risposte JSON

Abbini i corpi JSON a 201, 404 e altri codici.

Impostare codici di stato nelle risposte JSON è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 3 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.

Status Codes Tell the Story

Every HTTP response carries a status code that signals success or failure. Your JSON body explains the details, but the code sets the tone first. 🚦

The Default Is 200

When you return jsonify alone, Flask sends 200 OK. That is perfect for a successful read, but other actions deserve their own code.

Return a Tuple

To choose a code, return a tuple of the JSON response and the number. Flask reads the second element as the HTTP status to send.

return jsonify(message="created"), 201

201 for Created

When a request creates a new record, reply with 201 Created. It tells the client the resource now exists, which 200 alone would not convey.

return jsonify(id=new_id), 201

404 for Not Found

If the requested item does not exist, return a JSON body with 404. The client learns the resource is missing, not that the server broke.

return jsonify(error="user not found"), 404

400 for Bad Input

When a client sends invalid data, answer with 400 Bad Request. Pair it with a JSON message that explains exactly what went wrong.

return jsonify(error="name is required"), 400

The 2xx Family

Codes in the 2xx range mean success. 200 reads, 201 creates, and 204 signals success with no body to send back.

The 4xx Family

Codes in the 4xx range blame the client. 400 means bad input, 401 means unauthenticated, and 403 means forbidden.

The 5xx Family

Codes in the 5xx range blame the server. A 500 means your code raised an unhandled error while processing an otherwise valid request.

Adding Headers Too

You can extend the tuple to three parts: body, status, and a headers dict. That lets you attach extra metadata in one clean return.

return jsonify(ok=True), 201, {"X-Created": "now"}

Consistency Builds Trust

Pick the right code for every outcome and use it everywhere. Consistent status codes make your API predictable and a joy to integrate.

Quick Check

Match the action to the most fitting HTTP status code.

Recap

You learned to pair JSON bodies with the right status code by returning a tuple, using 201, 404, and 400 to make your API clear and honest. ✅

Domande Frequenti

La lezione «Impostare codici di stato nelle risposte JSON» è gratuita?

Sì — il testo completo di «Impostare codici di stato nelle risposte JSON» è 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 «Impostare codici di stato nelle risposte JSON»?

Abbini i corpi JSON a 201, 404 e altri codici. 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 3 di 4.

Quanto tempo richiede la lezione «Impostare codici di stato nelle risposte JSON»?

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. Applicare jsonify a un dizionario
  2. Restituire liste e dati annidati
  3. Impostare codici di stato nelle risposte JSON
  4. Content-Type e header Accept
← Torna a Flask Academy