Skip to content
Sign inGet all access
Chapter 13·8 minFree

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:

  1. If curl reproduces it, the bug is in the request or the response.
  2. If curl does 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/
Output200 text/html 615

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)'
Output> GET / HTTP/1.1> Host: localhost:8080< HTTP/1.1 200 OK< Content-Type: text/html

Every byte

When -v is not enough, --trace-ascii adds offsets and counts:

terminal
curl -s --trace-ascii - localhost:8080/ -o /dev/null 2>&1 \
  | sed -n '6,11p'
Output=> Send header, 77 bytes (0x4d)0000: GET / HTTP/1.10010: Host: localhost:80800026: User-Agent: curl/8.7.1003e: Accept: */*004b:

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: close
OutputHTTP/1.1 200 OK

If a hand-typed request works and your client’s does not, capture what your client sends:

terminal
nc -l 9000 &
curl -s --max-time 1 localhost:9000/x
OutputGET /x HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*

What the exit code says

curl separates the three failures that look alike:

terminal
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=$?"
Outputrefused=7cert=60timeout=28
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: close
OutputHTTP/1.1 304 Not Modified

Drop 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:

terminal
{ 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/
Outputok

TLS bugs

s_client first — it separates TLS from HTTP in one command:

terminal
echo | openssl s_client -connect localhost:8443 2>/dev/null \
  | grep 'Verify return code'
OutputVerify return code: 18 (self-signed certificate)

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.