Skip to content
Sign inGet all access
Chapter 2·14 minFree

A Request by Hand

Typing the protocol into a socket, byte for byte.

Everything curl did in chapter 1, typed by hand.

The request line

Method, path, version — then headers, then a blank line:

GET / HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 200 OK

To send it, printf one argument per line and pipe it into nc:

terminal
printf '%s\r\n' \
  'GET / HTTP/1.1' \
  'Host: localhost' \
  'Connection: close' \
  '' | nc localhost 8080 | head -1
OutputHTTP/1.1 200 OK

The path is what follows the host, starting with /. It is never the full URL:

GET /nope HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 404 Not Found

An unknown method reaches the server and is rejected by it, not by the syntax:

BREW / HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 405 Not Allowed

A version the server does not speak gets its own code:

GET / HTTP/9.9
Host: localhost
Connection: close
OutputHTTP/1.1 505 HTTP Version Not Supported

CRLF, not LF

Every line ends with \r\n. The line endings are the one thing the protocol block cannot show, and nginx accepts bare \n anyway:

terminal
printf '%s\n' \
  'GET / HTTP/1.1' \
  'Host: localhost' \
  'Connection: close' \
  '' | nc localhost 8080 | head -1
OutputHTTP/1.1 200 OK

The blank line

Headers end with an empty line. Without it the server is still waiting:

GET / HTTP/1.1
Host: localhost
Connection: close

Nothing came back — not an error, just silence until the timeout. The blank line is how the server knows you have finished asking.

Host is mandatory

HTTP/1.1 requires it. Omitting it is a client error:

GET / HTTP/1.1
Connection: close
OutputHTTP/1.1 400 Bad Request

HTTP/1.0 predates virtual hosting and does not need one:

GET / HTTP/1.0
OutputHTTP/1.1 200 OK

Header names are case-insensitive, so this is the same request:

GET / HTTP/1.1
hOsT: localhost
connection: close
OutputHTTP/1.1 200 OK

The status line

Version, code, reason phrase. The path decides which:

GET /50x.html HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 200 OK

Response headers

nginx sends eight without being asked:

GET / HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615Last-Modified: Wed, 16 Apr 2025 12:55:34 GMTConnection: closeETag: "67ffa8c6-267"Accept-Ranges: bytes

Content-Length frames the body, ETag and Last-Modified drive caching in chapter 6, and Accept-Ranges offers partial downloads.

Connection: close

Without it, the server keeps the socket open for another request:

GET / HTTP/1.1
Host: localhost
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615Last-Modified: Wed, 16 Apr 2025 12:55:34 GMTConnection: keep-alive

Ask it to hang up and it says so:

GET / HTTP/1.1
Host: localhost
Connection: close
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615Last-Modified: Wed, 16 Apr 2025 12:55:34 GMTConnection: close

Chapter 7 reuses that open socket for a second request.

Sending a body

Content-Length says how many bytes follow the blank line:

POST / HTTP/1.1
Host: localhost
Content-Length: 11
Connection: close

hello=world
OutputHTTP/1.1 405 Not Allowed

In the shell the body is a second printf, after the one that writes the headers:

terminal
{ printf '%s\r\n' \
    'POST / HTTP/1.1' \
    'Host: localhost' \
    'Content-Length: 11' \
    'Connection: close' \
    ''
  printf 'hello=world'
} | nc localhost 8080 | head -1
OutputHTTP/1.1 405 Not Allowed

405 is the right answer: nginx serves static files and has nothing to do with a POST. The body was still read — the path exists, the method is refused:

POST /nope HTTP/1.1
Host: localhost
Content-Length: 0
Connection: close
OutputHTTP/1.1 404 Not Found

Getting it wrong

A request line that is not three parts:

GARBAGE
OutputHTTP/1.1 400 Bad Request

400 means the bytes were unparseable. Everything from 405 upward means the server understood you and declined.