So, I have this:
my $conn = await IO::Socket::Async.connect('127.0.0.1', 12340);
$conn.print: "GET /rest HTTP/1.1\r\n\r\n";
How to receive just the first line from the server ?
I could use whenever
and put some logic in it, but there's a simpler way, right ?
If you really only want the first line, and don't care about the rest of the response, then you can do this:
my $first-line = await $conn.Supply.lines.first;
That is, get the Supply
representing the response stream, have it split into lines (which results in a Supply
of lines), and take the first line that arrives. Any data received beyond the first line will be discarded.
IO::Socket::Async
works in terms of providing packets of data as they arrive. It doesn't get into providing a line-oriented interface; if a protocol really is so simple as reading line by line, then a react whenever $conn.Supply.lines -> $line { }
will do it (and handles lines split over packets correctly).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With