Core\Request::body
Core\Request::body(): tainted stringReturns the whole body of the request as one string: every byte the client sent after the headers.
Use it when you need the body exactly as it arrived. For example, you can check a signature, or store a document without changing it. The result is empty when the request has no body. You can call body as many times as you like, and post and json still work after it. After bodyStream or files, body throws a LogicError, because those two do not keep the body. A command-line program answers no request, so body throws a LogicError there too.
Good to know: the result is tainted, which means it came from outside your program. You cannot use it as the pattern of a regular expression or as other text that Novis parses. You can still print it, hash it, compare it and store it.
Return value
Returns tainted string. Every byte the client sent, in order and unchanged. The string is tainted. It is empty when the request has no body, and every call returns the same string.
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.ParseErrorThe body is not valid UTF-8, so it cannot be astring.bytesreturns the same body asbytes.
Examples
Reading what the client sent
<?nvs// Shows how to read the body of a request that sends a short note as plain text.
// `Core\Request::body` returns every byte the client sent, as one string.tainted string $note = Core\Request::body();
// This prints 68, the length of the note in characters.echo 'received ', Core\Str::length($note), " characters\n";
// The body is exactly what the client sent, line breaks included.echo $note;received 68 charactersCall the supplier about order 1042.Ask for delivery before Friday.A request without a body
<?nvs// Shows what `Core\Request::body` returns when the request has no body, like most GET requests.
tainted string $body = Core\Request::body();
// A request without a body gives the empty string. It does not throw an error.if ($body == '') { echo "no body was sent\n";} else { echo 'the body has ', Core\Str::length($body), " characters\n";}
// You can call `body` again. It returns the same result every time.echo Core\Request::body() == $body ? "same result\n" : "different result\n";no body was sentsame resultChecking a webhook signature
<?nvs// Shows how a shop checks that a payment webhook really comes from the payment service.
// The payment service and the shop both know this secret.bytes $secret = 'whsec_shop_payments' as bytes;
// The service signs the exact bytes of the body. So the check needs the raw body,// before anything decodes it.tainted string $body = Core\Request::body();tainted string $signature = Core\Request::header('x-signature') ?? '';
// Compute the signature again, and compare it with the one in the header.string $expected = Core\Encoding::toHex(Core\Hash::hmac($body, $secret, Core\Hash::Digest::Sha256));
// `Core\Hash::equals` takes the same time wherever two values differ.// This prints `accepted`.if (Core\Hash::equals($expected as bytes, $signature as bytes)) { echo 'accepted: ', $body, "\n";} else { echo "rejected\n";}accepted: {"order":1042,"status":"paid"}