Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Send a Response Body

You’ve successfully routed requests and sent headers. Now let’s send actual data!

The Message Body is the payload of an HTTP message. It comes immediately after the empty line (\r\n\r\n) that terminates the headers.

According to RFC 9110 Section 8.6, when a server sends a message body, it MUST tell the client exactly how many bytes the body contains using the Content-Length header.

If you omit this header, the client has no way to know when the response ends, and it will just hang there waiting for more data until the connection times out!

Your Task

Create a new route: /greet/{name}.

Extract the {name} from the URL, and return the greeting string Hello, {name}! as the Response Body. You must also include a Content-Length header set to the exact length of the greeting string in bytes.

Example Request: GET /greet/bob HTTP/1.1

Example Expected Response:

HTTP/1.1 200 OK\r\n
Content-Type: text/plain\r\n
Content-Length: 11\r\n
\r\n
Hello, bob!

Caution

The Content-Length header measures the number of bytes, not characters! For plain ASCII text this is the same, but if your server ever sends UTF-8 emojis, string.length() in many languages will return the wrong number. Always measure the byte array length!


Important

Run the below docker command to test your solution.

docker run \
  --rm \
  --add-host host.docker.internal:host-gateway \
  codeberg.org/level0/buildit/http-server:latest \
  --addr host.docker.internal:8080 \
  --until response_body