Skip to main content
Text ready — video coming soon

Read HTTP, JSON and configuration

Let's follow the chapter request beyond a single file. In the function lesson, you separated text input, a converted value and an error response. A website has the same need, but the input and response can travel between different programs.

This lesson follows one request for chapter information. You will learn to:

  • Identify the request and interpret its response status.
  • Distinguish JSON text from the Python values obtained by parsing it.
  • Find where a configuration value comes from.

We will describe the exchange before introducing any web framework, so you can check the information crossing each boundary.

Follow a request to the program answering it​

HTTP, Hypertext Transfer Protocol, is a set of rules for exchanging requests and responses. A client initiates a request. A server receives it and returns a response. Your browser is one possible client; Python code can be another.

Imagine a book service with an address ending in /chapters/1. The path is an example of the interface we want, not an address to a server running on your computer yet. Its request might begin:

GET /chapters/1 HTTP/1.1
Host: books.example
Accept: application/json

GET is the method, the action the request asks for. It is intended to retrieve information. /chapters/1 is the requested path. The remaining lines are headers, named pieces of information about the request. Accept tells the server which response format the client can use.

Think of checking a courier tracking number: you want the current status, not a change to the delivery address. That is the intention of a read-only request. The server must still implement the operation correctly; naming it GET cannot make a mistaken write harmless.

The response might be:

HTTP/1.1 200 OK
Content-Type: application/json

{"title": "Python and Roots", "lessons": 7}

200 is the status code. Content-Type describes the body format. The body contains the actual chapter data. A successful status does not prove the title or count is accurate; that still needs a behaviour check.

Read status and content as separate evidence​

You do not need to memorise every status code, but you should recognise what kind of outcome it represents:

StatusWhat it indicatesWhat to investigate
200A successful responseWhether the returned data meets the requirement
400A request the server considers invalidThe expected input and the supplied value
401Authentication is missing or unacceptableHow the caller proves its identity
403Access is refusedWhether this caller is allowed to perform the action
404The requested resource was not foundThe path or identifier, without inventing a result
429Too many requests under a limitThe limit and any instructed retry delay
500An unexpected server failureServer-side evidence, with private values redacted

Authentication establishes identity. Authorisation decides permission. A known identity does not automatically have access to every chapter, document or tool. A friendly error message is also not permission enforcement.

In real network work, responses can arrive late or not arrive. The caller needs a timeout, a bounded wait, and a clear failure outcome. Repeating a failed request is not always safe: a write operation may already have happened even if its response was lost. Our read-only chapter lookup avoids that write problem, but other solutions need to handle it deliberately.

Treat JSON as text that represents data​

JSON, JavaScript Object Notation, is a text format for a limited set of data shapes. Despite the name, Python and many other languages can read and produce it.

JSON resembles a Python dictionary, but it is not Python code:

{
"title": "Python and Roots",
"lessons": 7,
"available": true,
"video": null
}

Keys and text use double quotes. JSON uses true, false and null; the corresponding Python values are True, False and None. JSON does not permit comments or a trailing comma after the last entry.

Python's standard json module converts between JSON text and Python values:

import json

body = '{"title": "Python and Roots", "lessons": 7}'
chapter = json.loads(body)
print(chapter["lessons"] + 1)

loads reads JSON from a string. After conversion, chapter is a Python dictionary and its lesson count is an integer, so the last line displays 8. json.dumps(chapter) converts that Python data back into JSON text. Later, load and dump, without the final s, will work with an open file.

Parsing, reading text according to its format to obtain values, is only the first check. Valid JSON such as {"lessons": "seven"} still has the wrong value type for a numeric count. In Chapter 2, validation will check the shape and types we require. It will not decide whether a caller has permission to request the data.

Find the settings the process actually reads​

Configuration supplies values that can change without rewriting the program: a language choice, a permitted server address or a timeout. A process is a running instance of a program. Its environment variables are named text values available to that process.

For a harmless example, set a language value in the current PowerShell session:

$env:BOOK_LANGUAGE = "en"
uv run python chapter_config.py

Save this as chapter_config.py:

import os

language = os.environ.get("BOOK_LANGUAGE", "en")
print(language)

The program reads the value when that line executes. The second argument to get is the default if the setting is missing. Environment values are text: the setting "30" is not automatically the integer 30.

A .env file is a common way to store local settings, but ordinary Python does not load it merely because the file exists. Some libraries read it when configured to do so. When a setting seems ignored, check the actual loading code, the selected environment and whether the process needs restarting.

A missing optional language value can have a default. A missing required credential should not silently become a pretend working credential. That difference belongs in the specification before the AI chooses a default.

Safety floor

Keep credentials out of lesson files, code, commits, screenshots and shared logs. Read secrets through an approved configuration mechanism and avoid printing them. Choosing an environment variable reduces accidental source-code exposure; it does not make a secret safe from every process or person with access.

The HTTP overview, Python JSON reference and process environment reference support this lesson.

Check what crossed the boundary​

Try these before opening the answers:

  1. Can a 200 response still contain the wrong chapter title? What would prove correctness?
  2. Why is valid JSON not enough to establish a valid lesson count?
  3. An AI puts true into an ordinary Python dictionary. What distinction did it miss?
  4. Why might creating .env fail to change what a running Python process sees?
  5. What is the difference between proving the caller's identity and deciding what that caller may read?
Check your reasoning

The status describes the HTTP outcome, not the truth of every returned field. Compare the data against the approved chapter information. JSON parsing can succeed while a field has the wrong type. Python uses True, not JSON's true. A .env file needs an actual loader, and an already running process may retain old settings. Authentication establishes identity; authorisation checks permission for the requested action and resource.

Next, files and tests will make the chapter data persistent and give you repeatable checks for both good and bad inputs.