Handle getParsedBody on empty


Is there anyway to handle getParsedBody on empty or null?

I’m only passing {"firstname: “test”} to my api, so the middlename and lastname will get an error because there is no such thing as middlename and lastname in my request. How to handle this kind of situation? Thank you.

$firstname= $request->getParsedBody()[‘firstname’];
$middlename= $request->getParsedBody()[‘middlename’];
$lastname= $request->getParsedBody()[‘lastname’];


Check if the array key exists:

$parsedBody = $request->getParsedBody()
$firstname = $parsedBody['firstname'] ?? false;


define variabe
$shift = null;
$shift_keyword = ‘shift’;
$data = $request->getParsedBody();
if ($data) {
if (array_key_exists($shift_keyword, $data)) {
$shift = $data[$shift_keyword];


I prefer SVL solution but a little bit more safe:

$parsedBody = $request->getParsedBody()
$firstname = isset($parsedBody['firstname']) ?? false;

With isset you avoid a new entry in error log.