Core\Request::bytes
Core\Request::bytes(): tainted bytesReturns the whole body of the request as bytes: every byte the client sent after the headers, unchanged.
Use it when the body is a file or other data that is not text, for example an uploaded image or PDF. body returns a string, and a string must be valid UTF-8 text. When the body is not valid text, body throws an error and bytes still returns it. The result is empty when the request has no body. You can call bytes, body, post and json in any order, as many times as you like. After bodyStream or files, bytes throws a LogicError, because those two do not keep the body. A command-line program answers no request, so bytes throws a LogicError there too.
Good to know: the result is tainted, which means it came from outside your program. You can still measure it, hash it, compare it and store it.
Return value
Returns tainted bytes. Every byte the client sent, in order and unchanged. The value is tainted. It is empty when the request has no body. A body that is not valid UTF-8 is returned too, where body throws an error.
Errors
LogicErrorThe program is not handling a request. OrbodyStreamorfilesalready read the body, and those two do not keep it.RuntimeErrorThe body is larger than[limits] request_body. The bytes past that limit are never stored.IOErrorThe connection failed while the body arrived, or the client sent fewer bytes than itsContent-Lengthsaid.
Examples
Counting the bytes of a body
<?nvs// Shows how to read the body of a request as bytes and count them.
// `Core\Request::bytes` returns every byte the client sent, unchanged.tainted bytes $raw = Core\Request::bytes();
// `Core\Bytes::length` counts bytes. "è", "û" and "é" take two bytes each in UTF-8,// so this prints 28.echo 'bytes: ', Core\Bytes::length($raw), "\n";
// `Core\Str::length` counts characters in the same body. This prints 25.echo 'characters: ', Core\Str::length(Core\Request::body()), "\n";bytes: 28characters: 25A checksum of an upload
<?nvs// Shows how to compute a checksum of an uploaded file, so a later copy can be compared with it.
// The upload is the whole body of the request.tainted bytes $upload = Core\Request::bytes();
// `Core\Hash::of` computes a SHA-256 digest of the exact bytes that arrived.// The same file always gives the same checksum.string $checksum = Core\Encoding::toHex(Core\Hash::of($upload, Core\Hash::Digest::Sha256));
echo 'received ', Core\Bytes::length($upload), " bytes\n";echo 'sha-256 ', $checksum, "\n";received 33 bytessha-256 65bd3e7abaf815c6bf66264dcbc0d0918b661e44ebf3d03b5db0ce46841b503aAccepting only pdf uploads
<?nvs// Shows how a service that stores invoices checks that an upload really is a PDF file.
tainted bytes $upload = Core\Request::bytes();
// The `content-type` header can say anything. So the program checks the file itself.// Every PDF file starts with the bytes `%PDF-`.if (Core\Bytes::startsWith($upload, '%PDF-' as bytes)) { // This prints `stored invoice, 142 bytes`. echo 'stored invoice, ', Core\Bytes::length($upload), " bytes\n";} else { echo "rejected: the upload is not a PDF file\n";}stored invoice, 142 bytes