The Life Starter
0 of 6 complete0%
Course Library · Lesson 3 of 6
Digital · Guided lesson

Return JSON with correct headers

About 29 minutes · 7 reading + 22 practice and checks
Time includes reading, practice, and questions. Extra videos or further study are optional.
See this course’s project requirements

A localhost-only read-only JSON book API with defined routes, 200/404 responses, headers and an automated client contract test.

Keep your working files in your own notebook or software. Save a private description, checklist, and work link in My Proof.

What to include

  • A three-route API contract with status and typed body.
  • A loopback-only server boundary record.
  • A checked json response with matching headers.
  • A routing test matrix with exact-path boundaries.
  • A real local transport test with bounded cleanup.
  • A complete api handoff with a controlled contract change.

Review and revise

Use these criteria to check your work and choose what to improve.

  • The implementation or declared simulation follows the supplied contract and retains inspectable original inputs.
  • Normal, boundary and rejected cases are recorded with expected versus observed results.
  • The changed case has a correct explanation and does not rely on hidden state.
  • The project includes source, outputs, instructions and honest tool, privacy and verification limits.
Open the full course project
Want more background?
Before you begin

The idea in everyday life

Book 2 body is JSON containing id 2 and title River Map. The code uses json.dumps(data).encode("utf-8"), then len(body).

The idea

Encode JSON before writing bytes. Content-Length counts encoded bytes, not characters; Content-Type tells clients which representation to safely parse.

Follow along

Return JSON with correct headers — original worked practice

Encode JSON before writing bytes. Content-Length counts encoded bytes, not characters; Content-Type tells clients which representation to safely parse.

Book 2 body is JSON containing id 2 and title River Map. The code uses json.dumps(data).encode("utf-8"), then len(body).

  1. 1. Step 1

    Trace object→JSON text→UTF-8 bytes.

    You should see: wfile receives bytes rather than a Python dictionary.

    This keeps the rule and its observable result together.

  2. 2. Step 2

    Read the Content-Type header.

    You should see: application/json; charset=utf-8 declares the format.

    This keeps the rule and its observable result together.

  3. 3. Step 3

    Compare Content-Length with len(body).

    You should see: The header names exact encoded byte count.

    This keeps the rule and its observable result together.

  4. 4. Step 4

    Inspect the test assertions for headers and parsed body.

    You should see: A checked json response with matching headers.

    This keeps the rule and its observable result together.

Now change one thing

A json text contains anon-Ascii character that encodes as two bytes. Enter bytes or characters for the unit Content-Length counts.

Your work

What to finish

A checked json response with matching headers.

Common mistake

Watch for this

Measuring a JSON character count instead of the length of its encoded response bytes.

How can I tell I understand it?

Explain and demonstrate the required distinction: Encode JSON before writing bytes. Content-Length counts encoded bytes, not characters; Content-Type tells clients which representation to safely parse. Show a changed input and the expected result without weakening this contract.

Try 3 questions

Quick knowledge check

Answer all 3 questions. Get at least 2 right and the lesson completes automatically.

Not yet passed

You’ll get an explanation for each answer. Signed in? Your results save to your account.

1What does wfile.write expect here?
2What must Content-Length match?
3Which header declares the JSON representation?
0 of 3 answeredApply the lesson to a short question, then review the explanation.
3
Finish this lesson

Pass the check. We’ll save the progress.

Answer all 3 questions above and get at least 2 right. You can review the explanations and try again.

Go to the knowledge check
Tools, templates, and lesson sources

Tools and templates

Tools open in a new tab so you can keep your place here.

Sources and further reading

Explore the references behind this lesson.

docs.python.org — http.server.html
Source & access notes · checked 2026-10-01

Relevant documented module/API behavior inspected during expansion authoring; actual execution evidence is recorded separately. No grant eligibility is established.

Access: Official page retrieved with the web reader on 2026-10-01. Documentation reading does not certify execution in every native tool or browser.

Reuse: Original local practice files, fictional data, worked examples and questions. Concepts paraphrased and linked; source exercises are not copied.

docs.python.org — json.html
Source & access notes · checked 2026-10-01

Relevant documented module/API behavior inspected during expansion authoring; actual execution evidence is recorded separately. No grant eligibility is established.

Access: Official page retrieved with the web reader on 2026-10-01. Documentation reading does not certify execution in every native tool or browser.

Reuse: Original local practice files, fictional data, worked examples and questions. Concepts paraphrased and linked; source exercises are not copied.

Report a problem with this lesson

Tell us what went wrong. Please leave out personal information and private project contents.

No sign-in needed.