Sub topic 1
How a Browser and Crawler Request a URL
This chapter covers the request and response cycle that every later chapter builds on. You will see what a browser or crawler sends, what a server returns, and how to inspect both with curl.
Prerequisites
This chapter assumes you:
- Know what a URL is and how a browser loads a page.
- Can run commands in a terminal.
- Can read basic HTML links (
<a href="...">). - Have seen a web server such as nginx or Apache, or a CMS, at least once.
The request and response cycle
Fetching a URL takes a few steps. The client splits the URL into a scheme (https), a host (example.com), and a path (/old-page). It resolves the host to an IP address through DNS, opens a TCP connection, and negotiates TLS for HTTPS. It then sends an HTTP request. The server replies with a status line, headers, and usually a body.
What a request contains
A request has a method, a path, and headers. The method is usually GET, which asks for the page body. A HEAD request asks for the same headers without the body, so it is cheaper for checking many URLs. The Host header names the site, which lets one server host many sites. The User-Agent header identifies the client.
The response starts with a status line such as HTTP/1.1 404 Not Found. The three-digit status code tells the client what happened. Later chapters cover the codes in detail. For now, note that the first digit gives the class:
| Class | Meaning | Example |
|---|---|---|
| 2xx | Success | 200 OK |
| 3xx | Redirect to another URL | 301 Moved Permanently |
| 4xx | Client error, such as a bad URL | 404 Not Found |
| 5xx | Server error | 500 Internal Server Error |
Browsers and crawlers
Browsers and crawlers send the same kind of request. They differ in what they do afterward.
- A browser renders the response, loads images, CSS, and scripts, and follows redirects automatically. A person sees the final page and may never notice the hops.
- A crawler, such as Googlebot or an audit tool, records the status code of each URL. It extracts links from the HTML and queues them for later requests. It treats each redirect hop as a separate request.
Because of this difference, a link can look fine in a browser and still cost a crawler several requests. Seeing the raw responses is the first step in diagnosing broken links.
Seeing the exchange with curl
The -I flag sends a HEAD request. With -s (silent), you get only the response headers:
curl -sI https://example.com/old-pageA redirect response might look like this:
HTTP/2 301
location: /new-page
content-type: text/htmlThe location header is the destination. It can be relative, so a client must resolve it against the current URL. Add -v to see the request headers your client sent as well.
Some servers answer HEAD differently from GET. If a result looks wrong, repeat the check with GET before you trust it.
Exercise
A crawler requests /old-page and receives a 301 with a Location header of /new-page. How many requests has the crawler made when it finishes reading /new-page, and why?
Show answer
Two. The first request returns the redirect. The crawler then makes a second request to /new-page, because each redirect hop is a separate request.