Skip to content

L13 HTTP and routing

18 minSuggested first: C2 or equivalent

Outcome

Identify a request's method, path, query, and status, then select its handler under stated rules.

Learn

An HTTP request has a method and a target, and can carry headers and a body. A response has a status, and can carry headers and a body. A router connects a request to a handler.

In the toy router below, both the method and the path must match; query parameters don't choose the route.

Worked example

Which handler receives GET /books/42?format=short, and what is id?

A toy routerText
GET  /books/:id  → readBook
POST /books      → createBook
Other requests   → 404 in this toy router
Predict first, then show the worked explanation

GET /books/42?format=short matches the first rule, so readBook runs with id bound to the string "42". The format stays available as a query parameter. POST /books would match createBook instead.

Practice and check

Guided practice

Practice, not scored

Which handler receives POST /books?format=short?

Choose one answer

Exit check

Counts toward completion

Under the stated rules, what happens to POST /books/42?

Choose one answer

Common mistake

Assuming a framework's defaults are HTTP requirements. Check precedence, trailing slashes, and the fallback in the pinned implementation.

Practice on a real repository

Try one of the puzzles below.

Related topics: Routing

Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.

References