Sub topic 17
Inspecting traffic with curl and DevTools
This chapter assumes you know the structure of an HTTP request and response, the common status codes, and the role of headers, cookies, and caching. Here you will use two tools to observe those pieces on real traffic: curl in the terminal and the Network panel in browser DevTools.
Why inspect traffic
Most web bugs come down to a mismatch between what you think was sent and what was actually sent. A wrong header, an unexpected redirect, or a stale cached copy can all look like "the page is broken." Reading the real messages replaces guessing with evidence.
Watching an exchange with curl
The -v flag prints the request and response headers.
curl -v https://example.com/The output marks each line with a prefix:
| Prefix | Meaning |
|---|---|
| `*` | Connection information, such as DNS, TCP, and TLS steps |
| `>` | The request curl sent |
| `<` | The response curl received |
Because curl does not run JavaScript or render HTML, you see exactly what the server returned. That makes it a clean way to separate server problems from browser problems.
Other useful flags:
-Isends a HEAD request and prints only the response headers.-Lfollows redirects, so you can see where a 301 or 302 leads.-H "Name: value"adds a request header.-X POST -d "a=1"sends a POST with a body.
curl -I -L http://example.com/Inspecting with DevTools
Open DevTools with F12 (or Ctrl+Shift+I, or Cmd+Option+I on macOS), then choose the Network tab. Reload the page to record requests. Each row is one request, showing its method, status, type, size, and timing.
Click a row to see the details:
- Headers shows the request and response headers, including cookies sent and set.
- Preview and Response show the body.
- Timing breaks the request into stages such as DNS lookup, connection, TLS, waiting for the first byte, and download.
You can also use the browser's own client from the Console tab:
const response = await fetch("https://example.com/");
console.log(response.status);
console.log(response.headers.get("content-type"));Common diagnoses
- Unexpected 3xx: use
curl -ILand read eachLocationheader. - Stale content: check
Cache-ControlandETag. In DevTools, a size of "(disk cache)" means no request reached the server. A304means the browser revalidated. - Missing login state: compare the
Set-Cookieresponse with theCookieheader on the next request. Attributes such asSecureorPathcan stop a cookie from being sent. - Works in curl, fails in the browser: the cause is often CORS or the same-origin policy. These checks run only in browsers, and the browser Console reports them.
Exercise
A page shows an outdated logo, and curl -I on the logo URL shows "Cache-Control: max-age=3600". What would you check in DevTools, and what response confirms the browser revalidated instead of re-downloading?
Show answer
Check the logo's row in the Network tab. If it says "(disk cache)", the browser used its stored copy without contacting the server, which is allowed for up to 3600 seconds. After expiry, the browser sends If-None-Match, and a 304 Not Modified response confirms it revalidated and kept the stored copy.
Exercise
Which curl flag shows both the request headers sent and the response headers received?
Show answer
The -v flag. Lines starting with ">" are the request, and lines starting with "<" are the response.