HTTP: read a request like a log
Read real requests and responses with curl -v, know what each status code family blames, see where a request spends its time, and how HTTP/1.1, HTTP/2 and HTTP/3 differ.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: curl against example.com, github.com, httpbin.org and cloudflare.com from a Linux host. The
output shown is real.
Cloud lab: none needed.
Almost everything you deploy speaks HTTP, and every proxy, load balancer and app in between logs it. Being able to read a request and response quickly is most of debugging a web service.
Request line, headers, body
$ curl -v http://example.com -o /dev/null > GET / HTTP/1.1 > Host: example.com > User-Agent: curl/8.18.0 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: text/html; charset=utf-8 < Server: cloudflare < Age: 5275 < cf-cache-status: HIT
curl -v http://example.com -o /dev/nullcurlmake an HTTP request-owrite the body to this file
(Trimmed.) HTTP is plain text over the TCP connection from lesson 3: a request line, headers, a blank line, then an
optional body. Age: 5275 and cf-cache-status: HIT say this page came from Cloudflare's cache, 88 minutes old;
the real server was never asked.
Methods and status codes
GET reads, POST creates, PUT replaces, PATCH changes part, DELETE removes. GET, PUT and DELETE are
meant to be safe to retry; POST isn't, which is why a load balancer won't retry it for you. A POST with a JSON
body, echoed back by httpbin:
$ curl -s -X POST https://httpbin.org/post -H 'Content-Type: application/json' -d '{"order":42}' { "headers": { "Content-Length": "12", "Content-Type": "application/json", ... }, "method": "POST", "json": { "order": 42 }, ... }
curl -s -X POST https://httpbin.org/post -H 'Content-Type: application/json' -d '{"order":42}'curlmake an HTTP request-ssilent: no progress bar
The status code's first digit tells you whose problem it is:
$ curl -sIL http://github.com | grep -iE '^HTTP|^location' HTTP/1.1 301 Moved Permanently Location: https://github.com/ HTTP/2 200
curl -sIL http://github.com | grep -iE '^HTTP|^location'curlmake an HTTP request-ssilent: no progress bar-Iheaders only-Lfollow redirects|pipe: send output to the next commandgrepfind lines that match a pattern
curl -L follows redirects. This one sends plain HTTP to HTTPS, the most common redirect there is.
Headers that matter in operations
| Header | Why you care |
|---|---|
Host | Picks the site. Wrong Host, wrong site or 404 |
Location | Where a 3xx redirect sends you; loops show up here |
Cache-Control, Age | Whether a CDN or browser served a cached copy |
X-Forwarded-For | The real client IP, added by proxies (lesson 9) |
Strict-Transport-Security | Tells browsers to use HTTPS only |
Retry-After | How long to wait after a 429 or 503 |
Where the time goes
When "the site is slow", break the request into phases. curl -w prints each one:
$ curl -s -o /dev/null -w 'http %{http_version} dns %{time_namelookup}s connect %{time_connect}s tls %{time_appconnect}s first byte %{time_starttransfer}s total %{time_total}s\n' https://github.com http 2 dns 0.129716s connect 0.223123s tls 0.326634s first byte 0.430652s total 1.185359s
curl -s -o /dev/null -w 'http %{http_version} dns %{time_namelookup}s connect %{time_connect}s tls %{time_appconnect}s first byte %{time_starttransfer}s total %{time_total}s\n' https://github.comcurlmake an HTTP request-ssilent: no progress bar-owrite the body to this file-wprint these details after the request
Each number is measured from the start. Slow DNS points at the resolver, slow connect at distance or packet loss, slow TLS at certificate or handshake issues, and a long gap before the first byte at your app or database.
HTTP/2 and HTTP/3
$ curl -s --http1.1 -o /dev/null -w 'http %{http_version}\n' https://github.com http 1.1 $ curl -sI https://www.cloudflare.com | grep -iE '^HTTP|alt-svc' HTTP/2 200 alt-svc: h3=":443"; ma=86400
curl -s --http1.1 -o /dev/null -w 'http %{http_version}\n' https://github.comcurlmake an HTTP request-ssilent: no progress bar-owrite the body to this file-wprint these details after the request
curl -sI https://www.cloudflare.com | grep -iE '^HTTP|alt-svc'-Iheaders only|pipe: send output to the next commandgrepfind lines that match a pattern
Client and server agree on HTTP/2 during the TLS handshake (ALPN). A server offers HTTP/3 with the alt-svc header,
and browsers switch to it on later requests. Methods, status codes and headers don't change between versions, so
everything else in this lesson applies to all three.
Profile three sites
For three sites you use, record the HTTP version, status code and the five timing phases. Find one that redirects and one served from a CDN cache, and explain which phase dominates for each.
~/http.txtReveal one safe solution
$ for u in https://github.com https://example.com https://www.cloudflare.com; do curl -s -o /dev/null -w "$u %{http_code} http %{http_version} dns %{time_namelookup} connect %{time_connect} tls %{time_appconnect} ttfb %{time_starttransfer} total %{time_total}\n" $u done > ~/http.txt $ curl -sIL http://github.com | grep -iE '^HTTP|^location' >> ~/http.txt $ curl -sI https://example.com | grep -iE '^age|cache' >> ~/http.txt
for u in https://github.com https://example.com https://www.cloudflare.com; doforloop over a listdostart of what the loop runs each time
curl -sIL http://github.com | grep -iE '^HTTP|^location' >> ~/http.txtcurlmake an HTTP request-ssilent: no progress bar-Iheaders only-Lfollow redirects|pipe: send output to the next commandgrepfind lines that match a pattern>>append output to a file
Run it twice. The second run is often faster because DNS is cached.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
A load balancer returns 502 Bad Gateway. Where do you look first?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.