Debugging HTTP
A method for any HTTP bug, and a checklist to work down.
Given a symptom: which command, and which chapter.
The method
Reproduce it with curl, then remove curl:
- If
curlreproduces it, the bug is in the request or the response. - If
curldoes not, the bug is in your client — or in the browser’s rules.
curl -s -o /dev/null -w '%{http_code} %{content_type} %{size_download}\n' \
localhost:8080/Three facts, no output to read — usually enough to decide which half you are in.
curl -v
The conversation from chapter 1. > sent, < received:
curl -sv localhost:8080/ -o /dev/null 2>&1 \
| grep -E '^[<>] (GET|Host|HTTP|Content-Type)'Every byte
When -v is not enough, --trace-ascii adds offsets and counts:
curl -s --trace-ascii - localhost:8080/ -o /dev/null 2>&1 \
| sed -n '6,11p'Use it when a byte count is wrong, a header holds invisible whitespace, or chunked framing does not add up.
Request or response?
The question that halves the problem. nc removes curl too:
GET / HTTP/1.1
Host: localhost
Connection: closeIf a hand-typed request works and your client’s does not, capture what your client sends:
nc -l 9000 &
curl -s --max-time 1 localhost:9000/xWhat the exit code says
curl separates the three failures that look alike:
curl -s -o /dev/null http://localhost:9999/; echo "refused=$?"
curl -s -o /dev/null https://localhost:8443/; echo "cert=$?"
curl -s -o /dev/null --max-time 1 http://10.255.255.1/; echo "timeout=$?"| Code | Means | Look at |
|---|---|---|
7 |
connection refused | is it listening, is the port right |
28 |
timed out | firewall, or the server is hung |
60 |
certificate | chapter 10 |
0 |
HTTP worked | the status code, not the connection |
Caching bugs
Ask for an unconditional response and compare:
GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: closeDrop the header and the same URL returns the body. If those disagree with what your application sees, a cache is between you.
CORS bugs
curl cannot reproduce one, which is the diagnosis:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Content-Length: 3\r\n\r\nok\n'
} | nc -l 9000 >/dev/null &
curl -s -H 'Origin: https://evil.example' localhost:9000/TLS bugs
s_client first — it separates TLS from HTTP in one command:
echo | openssl s_client -connect localhost:8443 2>/dev/null \
| grep 'Verify return code'A non-zero verify code means no HTTP happened at all.
The checklist
Work down it; the first surprising answer is the bug:
| # | Check | Chapter |
|---|---|---|
| 1 | Does the name resolve and the port answer? | exit code 7 |
| 2 | Does s_client verify? |
10 |
| 3 | Is the request line what you think? | 2 |
| 4 | Is Host right? |
2 |
| 5 | Are the request headers all present, once each? | 5 |
| 6 | Is the status code what the server meant? | 4 |
| 7 | Is the body framed by length or chunks? | 5 |
| 8 | Is something answering 304 that should not? |
6 |
| 9 | Is a redirect being followed silently? | 4 |
| 10 | Are credentials in the header, not the URL? | 9 |
| 11 | Does the browser fail where curl succeeds? |
11 |
The first two checks are not HTTP at all, and that ordering is deliberate: most “HTTP bugs” turn out to be a connection or a certificate.