HTTP, JSON, APIs, secrets, and failure handling

Handle service failures with clear retry and stop rules.

Visual lessons: 1 · 1:44 total Read estimate: 5 min Practice estimate: 8 hr Updated: 2026-10-03
In this lesson · B2 of 12All lessons in this track →

Progress for this track is saved in this browser only. No completion badge is issued. View your learning.

Start with the idea

Handle service failures with clear retry and stop rules.

Some failures may be temporary; others need a correction before another attempt. Limit both attempts and waiting time. Do not repeat a write if it might create a duplicate effect.

Retry budget: The maximum attempts and waiting time allowed for a request.

See the example and check your understanding

A made-up teaching example

The mock returns rate-limit and service errors, bad JSON and an authentication error. The exercise retries selected temporary failures within its budget. Bad JSON fails its format check; the authentication error stops immediately.

Ask yourself

Should every failure be retried until it works?

Show the explanation

No. Follow the declared rules. Protect repeated writes, respect the deadline and keep secrets out of logs.

Start with these checks

  • Use the allowed attempt and time limits.
  • Separate temporary and contract failures.
  • Log outcomes rather than secret-bearing content.

Use the different practice case below and check every requirement in its full instructions.

Go to the practice task ↓

The video explains the main idea. Use the sources, instructions and checks below to complete the practice.

Visual lesson1:44

Captions appear here during playback.

HTTP, JSON, APIs, secrets, and failure handling

Handle service failures with clear retry and stop rules.

Download transcriptOpen video file

Source-based teaching example · Fictional data · Reviewed 2026-10-03. Microsoft Mark · locally synthesized. On a small screen, use fullscreen for diagram details or read the transcript.

Read the descriptive transcript

0:00 · What you will learn

Handle service failures with clear retry and stop rules.

What appears on screen

Handle service failures with clear retry and stop rules.

0:06 · The idea

Some failures may be temporary; others need a correction before another attempt. Limit both attempts and waiting time. Do not repeat a write if it might create a duplicate effect.

What appears on screen

Some failures may be temporary; others need a correction before another attempt. Limit both attempts and waiting time. Do not repeat a write if it might create a duplicate effect.

0:21 · One useful term

Retry budget. The maximum attempts and waiting time allowed for a request.

What appears on screen

The maximum attempts and waiting time allowed for a request.

0:29 · A worked example

The mock returns rate-limit and service errors, bad JSON and an authentication error. The exercise retries selected temporary failures within its budget. Bad JSON fails its format check; the authentication error stops immediately.

What appears on screen

The mock returns rate-limit and service errors, bad JSON and an authentication error. The exercise retries selected temporary failures within its budget. Bad JSON fails its format check; the authentication error stops immediately.

0:48 · Your turn to think

Should every failure be retried until it works? Pause and decide before continuing.

What appears on screen

Should every failure be retried until it works? Pause the video if you need more time.

1:05 · Why this answer works

No. Follow the declared rules. Protect repeated writes, respect the deadline and keep secrets out of logs.

What appears on screen

No. Follow the declared rules. Protect repeated writes, respect the deadline and keep secrets out of logs.

1:15 · Check 1

Use the allowed attempt and time limits.

What appears on screen

Use the allowed attempt and time limits.

1:21 · Check 2

Separate temporary and contract failures.

What appears on screen

Separate temporary and contract failures.

1:27 · Check 3

Log outcomes rather than secret-bearing content.

What appears on screen

Log outcomes rather than secret-bearing content.

1:33 · Now try a different case

Open the lesson and try its different practice case. Use every check in the full task. Watching alone does not mark the lesson complete.

What appears on screen

Open the lesson Try the new case Check and revise The full task and its requirements are in the linked lesson.

Watching explains the example. Complete the lesson practice to check what you can do. Explore all visual lessons →

What you will be able to do

  1. Interpret request/response behavior and status codes.
  2. Implement bounded retry and timeout decisions.
  3. Keep credentials and protected bodies out of logs.

Prerequisites: dictionaries, loops, functions, exceptions and installed Python or paper tracing. Reuse earlier Builder artifacts only when the task requires them.

This curriculum runs self-study on VetsLearn. Where the text calls for an instructor, peer or formal review, use the stated acceptance criteria for self-review. Reflection questions support reflection; they do not replace independent assessment. A peer review is optional unless your actual organization requires it.

Part 1 Technical instruction

An API client is a reliability and security boundary. A request needs a method, endpoint, headers, payload, timeout, and request identifier. Responses have transport success, protocol status, and application semantics; a 200 response can still contain invalid JSON or an invalid business object. Retry only transient failures, respect Retry-After, add jitter to avoid synchronized retries, cap attempts and total time, and never retry a non-idempotent action without an idempotency design. Secrets enter through an approved runtime mechanism and must never be returned in exceptions or telemetry.

Mechanism and implementation

Separate transport, protocol and application outcomes. Retry policy depends on operation semantics as well as status, deadline, jitter and Retry-After. A timed-out write may already have succeeded: reconcile before retrying. Authentication failures require corrected credentials or a human decision.

Constructed offline example

Save as topic_example.py and run python topic_example.py with an installed Python 3.11+ interpreter. This standard-library construction uses synthetic values; its checks are not provider measurements.

def next_action(status, method, key=None, idempotency_supported=False):
    if status == 401:
        return "stop_auth"
    if status in {429, 503}:
        safe_replay = method == "GET" or (key is not None and idempotency_supported)
        return "retry" if safe_replay else "reconcile"
    return "validate" if status == 200 else "stop"
assert next_action(503, "GET") == "retry"
assert next_action(503, "POST") == "reconcile"
assert next_action(503, "POST", key="case-1") == "reconcile"
assert next_action(503, "POST", key="case-1", idempotency_supported=True) == "retry"
assert next_action(401, "GET") == "stop_auth"
print("retry classification passed")

The Boolean represents a separately verified server contract for this constructed exercise. Sending an arbitrary idempotency header is insufficient: the server must enforce replay identity, scope and retention for that operation. This classifier does not implement deadlines, backoff or durable recovery.

Engineering decisions

  • Classify errors as retryable, non-retryable, or indeterminate.
  • Set timeout, attempt, and total-time budgets.
  • Choose a structured error that helps operations without exposing the request body.

Worked example

A mock service returns 429, 503, malformed JSON, and 401 in sequence. The client retries the first two within a three-attempt budget, rejects malformed JSON as a contract error, and stops immediately on 401. Logs contain request ID, outcome class, attempt, and elapsed time - not tokens or bodies.

Diagnose the failure

Unprotected writes cannot automatically retry. An excessive wait exhausts the deadline. Log attempts and status, never secret-bearing bodies; if attempts exceed the cap, repair the guard.

Part 2 Guided practice

Trace GET 503 then 200 and POST 503 without a key. Add a fake clock, three-attempt cap and total deadline. Independently test a Retry-After longer than the remaining deadline.

Answer criteria and feedback: Unprotected writes cannot automatically retry. An excessive wait exhausts the deadline. Log attempts and status, never secret-bearing bodies; if attempts exceed the cap, repair the guard.

Continue with the downloadable pack below. If the pack or interpreter is unavailable, trace this self-contained example by hand in an input/expected-status/controlling-rule table and label execution unverified. Do not invent passing output. No account, credential, paid call, purchase or actual deployment is required.

Part 3 Remediation

Use the deterministic fault sequence. If a test hangs, inspect the total-time budget and sleep abstraction. Replace real waits with the injected fake clock. After repair, reassess with the alternate scenario in scenario_alt.json. A critical authorization, data-handling, evidence-integrity, or prohibited-action failure cannot be averaged into successful practice; it requires targeted remediation.

Part 4 Sources and currency

Use the workflow decision reference for model/deployment/harness distinctions and controlled comparisons. Source currency must be checked before each cohort. Provider/product behavior is illustrative where mocked; resolve current interfaces only from official documentation in an authorized environment.

Lesson status:PRACTICE REVIEW REQUIRED

Review your practice