Course of Raku / Functional, concurrent, reactive, and web programming / Web programming / A simple HTTP client / Exercises / Status with a client

Solution: Status with a client

Here is a possible solution to the task.

Code

use Cro::HTTP::Client;

my $response = await Cro::HTTP::Client.get('http://example.com/');
say $response.status;

🦋 You can find the source code in the file status-line.raku.

Output

200

Comments

  1. Cro::HTTP::Client.get returns a promise — a network request finishes later — so we await it.

  2. The response object already knows its .status, so we get 200 directly, without sending the request text or parsing the reply by hand. Compare this with the raw-socket version: the module does all the protocol work for you.

Course navigation

Status with a client   |   A simple HTTP server