Latticework

Command Palette

Search for a command to run...

HTTP

Requests & Responses

16 min

Explanation

Every HTTP exchange is a request (client → server) followed by a response (server → client), each with the same basic shape: a start line, headers, and an optional body. A request's start line has three parts:

GET /api/users HTTP/1.1

GET (the method — what kind of action), /api/users (the path — which resource), HTTP/1.1 (the protocol version).

def parse_request_line(line):
    method, path, version = line.split(" ")
    return method, path, version
Try it

The method tells you the INTENT (GET = read, POST = create, DELETE = remove...) -- REST APIs lean heavily on this convention rather than encoding the action into the path itself.

Loading editor…
Explanation

A response's start line begins with a status code — a 3-digit number whose FIRST digit tells you the general category:

| Range | Meaning | |---|---| | 2xx | Success (200 OK, 201 Created) | | 3xx | Redirection (301 Moved, 304 Not Modified) | | 4xx | Client error (404 Not Found, 401 Unauthorized) | | 5xx | Server error (500 Internal Server Error) |

def status_line_ok(status_code):
    return 200 <= status_code < 300

A 4xx means "you (the client) did something wrong" — a 5xx means "the server broke, and it wasn't your request's fault." That distinction matters a lot when debugging: a 4xx points you at your request, a 5xx points you at the server.

Exercise

Write `parse_request_line(line)`: split a raw HTTP request line like `'GET /api/users HTTP/1.1'` into `(method, path, version)`.

Exercise

Write `status_line_ok(status_code)`, returning True if the status code is in the 2xx success range (200-299).

Quiz

What HTTP status code range indicates a successful response?

Checkpoint

You can parse an HTTP request line into its three components, and classify a status code by its category.