Core\Http\Stream::chunks
$stream->chunks(): Core\Http\ChunksReads the body of a streamed reply piece by piece, exactly as the bytes arrive.
Each piece is tainted bytes, because another server sent it. The pieces are not split at lines or at any other boundary. How many pieces there are, and how large each one is, depends on the network. Your program only knows that the pieces together are the whole body, in order.
The body of a stream can be read only once. After chunks(), a call to chunks(), lines(), events() or saveTo() on the same stream throws a LogicError.
Good to know: you can stop the loop early with break. The rest of the body is then not read. Use saveTo() to write the body to a file.
The examples below count the size of a download, show that the body can be read only once, and stop a download that is larger than a limit.
Return value
Returns Core\Http\Chunks. An Iterable<tainted bytes> that a foreach can loop over once. The size of each piece depends on the network and has no meaning, so you cannot choose a size. If you need blocks of a fixed size, collect the bytes yourself.
Errors
LogicErrorThe body of this stream has already been read. The error message names the method that read it.RuntimeErrorThe reply's chunked encoding is broken.TimeoutErrorNo data arrived for longer thanidle, or the body took longer than itsmaxDuration.IOErrorThe connection failed while the body was arriving.
Examples
Counting the size of a download
<?nvs// Shows how to read a download piece by piece and count its size.
// The reply comes from this table, so nothing is sent over a network.Core\Test::answerHttp('https://files.example.com/report.csv', 200, { body: "date,sales\n2026-09-01,120\n2026-09-02,95\n" });
Core\Http\Stream $download = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://files.example.com/report.csv');uint $size = 0;// `chunks()` gives each piece of the body as it arrives.foreach ($download->chunks() as tainted bytes $piece) { $size = $size + Core\Bytes::length($piece);}// This prints "received 40 bytes".echo 'received ', $size, " bytes\n";received 40 bytesThe body is read once
<?nvs// Shows that the body of a stream can be read only once.
Core\Test::answerHttp('https://files.example.com/logo.png', 200, { body: 'PNG image data' });
Core\Http\Stream $image = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://files.example.com/logo.png');uint $size = 0;foreach ($image->chunks() as tainted bytes $piece) { $size = $size + Core\Bytes::length($piece);}// This prints "14 bytes".echo $size, " bytes\n";
// The body was already read, so a second call to `chunks()` throws a `LogicError`.try { foreach ($image->chunks() as tainted bytes $again) { echo "the body was read again\n"; }} catch (LogicError $error) { echo "the body cannot be read twice\n";}14 bytesthe body cannot be read twiceStopping a download that is too large
<?nvs// Shows how to stop a download when it becomes larger than a limit you choose.
// This server sends 60 bytes. The program allows at most 32.Core\Test::answerHttp('https://images.example.com/avatar.jpg', 200, { body: Core\Str::repeat('x', 60) });
uint $limit = 32;uint $received = 0;bool $tooLarge = false;Core\Http\Stream $avatar = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://images.example.com/avatar.jpg');foreach ($avatar->chunks() as tainted bytes $piece) { $received = $received + Core\Bytes::length($piece); // `break` stops the loop. The rest of the body is not read. if ($received > $limit) { $tooLarge = true; break; }}
// This prints "the image is larger than 32 bytes".if ($tooLarge) { echo 'the image is larger than ', $limit, " bytes\n";} else { echo 'the image has ', $received, " bytes\n";}the image is larger than 32 bytes