Latticework

Command Palette

Search for a command to run...

HTTP

Headers

16 min

Explanation

Headers are key-value metadata attached to a request or response — everything about the exchange EXCEPT the actual body content: what format the body is in, how large it is, whether it's cached, who's asking, and much more.

def parse_headers(lines):
    headers = {}
    for line in lines:
        key, value = line.split(": ", 1)
        headers[key] = value
    return headers

raw = ["Content-Type: application/json", "Content-Length: 128", "Host: api.example.com"]
print(parse_headers(raw))
Try it

split(': ', 1) with maxsplit=1 matters here -- some header VALUES contain colons themselves (like a URL or a timestamp), so splitting on every colon would break those.

Loading editor…
Explanation

Content-Type is one of the most important headers — it tells the receiver exactly how to interpret the body: application/json for JSON, text/html for HTML, multipart/form-data for file uploads, and so on. A server that receives a request without checking Content-Type risks trying to parse the wrong format entirely.

def is_json_request(headers):
    content_type = headers.get("Content-Type", "")
    return "application/json" in content_type

Other common headers worth knowing: Authorization (credentials — usually Bearer <token>), Accept (what response formats the CLIENT can handle), User-Agent (identifies the client software), and Cache-Control (covered in depth next module).

Exercise

Write `parse_headers(lines)`: given a list of `'Key: Value'` strings, return a dict mapping key to value.

Exercise

Write `is_json_request(headers)`: return True if the `Content-Type` header (if present) contains `'application/json'`.

Quiz

What does the Content-Type header tell the receiver of a request or response?

Checkpoint

You can parse raw header lines into a usable dict, and check a request's Content-Type to determine how to handle its body.