Network-shaped failures without fragile parsing

Intermediate Defensive APIs: validation, sanitization & exceptions
Created by Pavel · 29.04.2026 at 19:10 UTC

HTTP layers deliver bytes on the wire; JSON parsers want text; base64 wraps binary inside ASCII. Mixing these causes classic bugs: calling .json() on undecoded bytes, double-decoding UTF-8, or assuming Content-Type matches the body.

Robust clients: read bytes → decode with declared charset → json.loads → validate with Pydantic or manual checks. When scraping or using partner APIs, separate transport, parsing, and validation so failures pinpoint the broken layer.

requests response content vs text: [1].


Sources

University approvals: 0
Tasks
Question 1

In requests, response.content vs response.text differs because:

Hint

Bytes vs decoded str.

Question 2

You receive base64-encoded UTF-8 JSON inside a string field. The safest layering is:

Hint

Decode then parse then validate.

Question 3

decode_b64_ascii(b: bytes) -> bytes uses base64.b64decode (standard library).

Hint

stdlib base64 only.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Defensive APIs: validation, sanitization & exceptions
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Pavel
Pavel