L16 Clients and redirects
19 minSuggested first: L15 Input validation and dependencies
Outcome
Separate request preparation from sending and trace a redirect under a given policy.
Learn
An HTTP client may combine per-call options with defaults, prepare a request, and choose a
transport. A redirect response can make the client create another request. For each hop,
check the status, the Location, the method, and the origin. An origin is the scheme, host,
and port together.
Don't assume all redirect codes, clients, or credential types behave alike.
Worked example
The client sends POST https://a.example/submit and gets a 303 with Location: https://b.example/receipt. What is the next request?
prepare POST → send → 303 Location on a different origin
apply toy redirect policy → prepare GET with no body
remove Authorization for changed origin → send next hop
Predict first, then show the worked explanation
Under this toy policy, a 303 after a POST makes the next request a GET to the new URL with no
body, and a changed origin removes the Authorization header. So the next hop is
GET https://b.example/receipt, without the original body or Authorization. Compare the
general status semantics in RFC 9110.
Practice and check
Common mistake
Treating a redirect as a retry. A redirect follows a response and may change the target or the method; a retry makes another attempt at the same operation.
Practice on a real repository
Start with the first puzzle below, then try the second.
- From requests.get to a PreparedRequestTrace · Easy · psf-requests
- Dropping credentials on cross-origin redirectsFind · Medium · encode-httpx
Related topics: HTTP clients
Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.
References
- RFC 9110: HTTP Semantics (IETF)
- Fetch Standard (WHATWG)