0Pricing
Flask Academy · Lesson

Set Status Codes on JSON Replies

Pair JSON bodies with 201, 404, and friends.

Set Status Codes on JSON Replies is a free Flask Academy lesson on CoddyKit — lesson 3 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.

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. ✅

Frequently asked questions

Is the “Set Status Codes on JSON Replies” lesson free?

Yes — the full text of “Set Status Codes on JSON Replies” 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 “Set Status Codes on JSON Replies”?

Pair JSON bodies with 201, 404, and friends. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Set Status Codes on JSON Replies” 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

  1. jsonify a Dictionary
  2. Return Lists and Nested Data
  3. Set Status Codes on JSON Replies
  4. Content-Type and the Accept Header
← Back to Flask Academy