Course of Raku / Functional, concurrent, reactive, and web programming / Web programming / A simple HTTP server / Exercises / An HTML response

Solution: An HTML response

Here is a possible solution to the task.

Code

my $listener = IO::Socket::INET.new(
    :listen,
    :localhost('127.0.0.1'),
    :localport(8080),
);

my $conn = $listener.accept;
$conn.recv;

$conn.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>Hello, web!</h1>");
$conn.close;
$listener.close;

🦋 You can find the source code in the file text-response.raku.

Output

Hello, web!

Comments

  1. The response is a status line, a header, a blank line, and then the body — exactly the format the client side reads.

  2. The only change from a plain-text reply is the Content-Type: text/html header and an HTML body. That header tells the browser to render <h1>…</h1> as a heading rather than show the tags as literal text.

Course navigation

An HTML response   |   Cro 101