Request and response
A browser asks, a server answers. Both messages are plain text with the same shape: a first line, headers, a blank line and an optional body.
GET /index.html HTTP/1.1
Host: example.com
Accept: text/html
User-Agent: Mozilla/5.0HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Cache-Control: max-age=3600
Content-Length: 1256
<!doctype html>
...The method (GET) says what the browser wants to do: GET reads, POST sends data, for example a form.
Status codes
The three digits sort every response into one of five classes.
Class | Meaning | Examples |
|---|---|---|
1xx | Information, keep going | 100 Continue |
2xx | Success | 200 OK, 201 Created, 204 No Content |
3xx | Look elsewhere | 301 Moved Permanently, 302 Found, 304 Not Modified |
4xx | The request has a problem | 400 Bad Request, 403 Forbidden, 404 Not Found, 429 Too Many Requests |
5xx | The server has a problem | 500 Internal Server Error, 503 Service Unavailable |
A 3xx answer carries a Location header with the new address, and the browser follows it on its own.
Headers worth knowing
content-type: what the body is, such as
text/htmlorapplication/json.cache-control: how long the browser may reuse the response without asking again.
location: where a redirect points.
set-cookie: a small value the browser stores and sends back with later requests.
strict-transport-security: tells the browser to use HTTPS for this site from now on.
Exercise
Open the HTTP Header Analyzer and request
https://example.com. Which status code comes back, and what is the content-type?Request the same address with
http://instead. Is there a redirect? Which header gives it away?Request an address that does not exist on a site you know, such as
/does-not-exist. Which class does the status code belong to?Find one header in a response that is not on the list above and look up what it does.
