Core\Http\Response::bytes
Returns the body of a reply exactly as it arrived, as bytes.
Use bytes() for a reply that is not text, for example an image, an archive or a PDF file. It returns every byte of the body and checks nothing. text() returns the same body as a string, but it throws a RuntimeError when the body is not valid UTF-8 text. You can call bytes() more than once, and each call returns the same body.
The body comes from another server, so the result is tainted until your program checks it.
Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. They read a small image, show a body that text() cannot read, and check a downloaded profile picture before it is used.
Return value
Returns tainted bytes. Every byte of the body. It is tainted, for the same reason as text. The bytes are not checked. A reply that is not UTF-8 is returned here, but text throws an error for it.
Examples
Reading an image
<?nvs// Shows how to read the body of a reply that is not text, here a small image.
// `Core\Test::answerHttp` gives a fixed reply to this address, so the example runs// without a network. The body is the first eight bytes of a PNG image.Core\Test::answerHttp('https://cdn.example.com/logo.png', 200, { body: Core\Encoding::fromHex('89504e470d0a1a0a'), headers: ['Content-Type' => 'image/png'],});
Core\Http\Response $response = Core\Http\Client::get('https://cdn.example.com/logo.png');
// `bytes()` returns the body exactly as it arrived. This prints 8.tainted bytes $image = $response->bytes();echo Core\Bytes::length($image), "\n";
// This prints the same bytes as hex digits: 89504e470d0a1a0a.echo Core\Encoding::toHex($image), "\n";889504e470d0a1a0aA body that is not text
<?nvs// Shows the difference between `text()` and `bytes()` for a body that is not// valid UTF-8 text.
// The reply has three bytes. The byte ff never occurs in UTF-8 text.Core\Test::answerHttp('https://files.example.com/raw', 200, { body: Core\Encoding::fromHex('41ff42'),});Core\Http\Response $response = Core\Http\Client::get('https://files.example.com/raw');
// `text()` checks that the body is UTF-8 text. This body is not, so the call// throws a `RuntimeError`.try { echo $response->text(), "\n";} catch (RuntimeError $error) { echo 'text() failed: ', $error->message, "\n";}
// `bytes()` does not check the body. This prints 3.echo Core\Bytes::length($response->bytes()), "\n";text() failed: Core\Http\Response::text: the reply's body is not valid UTF-8, so it is not a `string` — byte 1 is where it stops being text, and `bytes()` reads it whole3Checking a download
<?nvs// Shows a program that downloads a profile picture and checks it before it// saves it. The picture must be a PNG image and must not be too large.
class Avatar{ // Every PNG image starts with these eight bytes. private static function pngStart(): bytes { return Core\Encoding::fromHex('89504e470d0a1a0a'); }
// Returns the picture's bytes. Throws a `RuntimeError` when the picture is not usable. public static function download(string $url): tainted bytes { Core\Http\Response $response = Core\Http\Client::get($url); tainted bytes $picture = $response->bytes(); if (!Core\Bytes::startsWith($picture, Avatar::pngStart())) { throw new RuntimeError($url . ' is not a PNG image'); } if (Core\Bytes::length($picture) > 100000) { throw new RuntimeError($url . ' is larger than 100000 bytes'); } return $picture; }}
// Two fixed replies, so the example runs without a network. The second one is// a web page, not an image.Core\Test::answerHttp('https://img.example.com/kim.png', 200, { body: Core\Encoding::fromHex('89504e470d0a1a0a0000000d49484452'),});Core\Test::answerHttp('https://img.example.com/lee.png', 200, { body: '<html>Not found</html>' });
// This prints `kim.png: 16 bytes`, then the error for lee.png.array<string> $names = ['kim.png', 'lee.png'];foreach ($names as string $name) { try { tainted bytes $picture = Avatar::download('https://img.example.com/' . $name); echo $name, ': ', Core\Bytes::length($picture), ' bytes', "\n"; } catch (RuntimeError $error) { echo $error->message, "\n"; }}kim.png: 16 byteshttps://img.example.com/lee.png is not a PNG image