PythiloLesson 1 of 5 · progress kept in this browser

HTTP and JSON

HTTP is text exchanged in pairs. A request has a method, a path, headers and an optional body. A response has a status code, headers and a body.

  • GET reads, POST creates, PUT replaces, DELETE removes.
  • 2xx means success, 4xx a client mistake, and 5xx a server failure.
  • Content-Type says how to interpret the body.

Most APIs send JSON. The json module converts between JSON text and Python values.

import json

text = json.dumps({"id": 1, "ok": True})
print(text)  # {"id": 1, "ok": true}
data = json.loads(text)
print(data["ok"])  # True
Your task

Write make_response(data, status=200) returning a dict with status, headers ({"Content-Type": "application/json"}) and body as compact JSON text with sorted keys. Write read_response(response) returning the parsed body, raising ValueError for status 400 or above (including the body's error value if it has one) or for invalid JSON.