Course outline · 0% complete

0/25 lessons0%

Course overview →

Anatomy of an HTTP response

lesson 4-2 · ~10 min · 11/25

What comes back

The server answers with the same text format, just a different first line:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1256

<html> ...the page itself... </html>
  1. Status line: VERSION CODE REASON. The status code 200 is the machine-readable result, and OK is the human-readable label.
  2. Headers, again Name: value. Content-Type says what the body is, because the client handles each format differently: text/html is a web page to render, image/png is picture bytes to display, and application/json is JSON (JavaScript Object Notation — a plain-text format for structured data that looks like {"user": "ada"}, and the standard language of APIs; lesson 4-4 covers it properly). Content-Length says how many bytes the body is, so the client knows when it has everything.
  3. Blank line, then the body: the actual page, data, or image bytes.

Everything your browser shows you arrived as the body of some response. The headers are invisible to users but they are the first thing an engineer reads when debugging: they tell you what the server thinks it sent.

Code exercise · bash

Run this. A response is stored in a variable, and the script reads its status line and extracts the code, exactly what every HTTP client library does first. head -n 1 takes the first line, cut -d' ' -f2 takes the second space-separated field.

Code exercise · bash

Your turn. Extract three things from this stored response: the status code, the Content-Type value, and the Content-Length value. grep '^Content-Type:' finds the header line, and cut -d' ' -f2 keeps the value after the space.

Quiz

A response has Content-Type: application/json. What does that tell the client?