build-an-http-client / lesson-40.md
Lesson 40 · Redirects and cookies

Emit the Cookie header

The other half of the jar - sending its cookies back to the server on the next request as a single Cookie header. Today you produce that header from the jar.

The goal

Produce a Cookie header value from the jar as name-value pairs joined by a semicolon and space.

Start here - the target
TO DO
Scenario: Sending cookies back
Givena jar holding cookies a=1 and b=2
Whenthe Cookie header value is produced
Thenit is "a=1; b=2" - name=value pairs joined by "; ", in name order
Andan empty jar produces no Cookie header (an empty value that the client omits)
Background

The jar’s second job is to hand its cookies back. Unlike Set-Cookie (one header per cookie, with attributes), the request side sends all cookies in a single Cookie header, as name=value pairs joined by ; . A jar holding a=1 and b=2 produces a=1; b=2. Emit them in sorted name order so the header is deterministic - the same jar always produces the same bytes, the same discipline as sorted request headers and ordered form pairs.

An empty jar produces an empty string, and the client simply sends no Cookie header at all - there is nothing to say. With Set-Cookie parsed into the jar and the jar emitting Cookie, the client can carry a session across requests. The capstone puts it together: follow a redirect while carrying a cookie the first response set.

Make it work
// join every stored cookie as "name=value", separated by "; ".
// emit in sorted name order so the header is deterministic.
// an empty jar yields "" (and the client sends no Cookie header).
func (j *Jar) header() string {
// sort names; join "name=value" with "; "
}
CheckpointDONE
The jar emits a Cookie header carrying its stored cookies. Commit and stop here.