Course of Raku / Functional, concurrent, reactive, and web programming / Web programming / A simple HTTP client / Exercises / A GET request

Solution: A GET request

Here is a possible solution to the task.

Code

my $conn = IO::Socket::INET.new(:host('example.com'), :port(80));

$conn.print("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n");
my $response = $conn.recv;
$conn.close;

say $response.lines.first.words[1];

🦋 You can find the source code in the file get-request.raku.

Output

200

Comments

  1. The request line, the Host: header, and the closing blank line make a complete HTTP request.

  2. The reply’s first line is the status line, such as HTTP/1.1 200 OK. Splitting it with .words and taking index 1 gives just the status code, 200 — the kind of text-parsing a client module would otherwise do for you.

Course navigation

A GET request   |   Status with a client