JSON Data CLI Post with Slim 3


#1

Hello,

CLI Command:

curl -X POST "https://api.domain.com/vm/create" -H "X-Auth-Key: Bearer 8fbb80d7f378f2271236388093439422" --data '{"usname":"example"}'

Code:

$app->post('/user/create', function ($request, $response, $args) {
	$json = $app->request->getBody();
	$data = json_decode($json, true); 
	$sth = $this->mdb->prepare("INSERT INTO `user` (`username`,`date`) VALUES (:uname,'0000-00-00 00:00:00')");
	$sth->bindParam("uname", $data['usname']);
	$sth->execute();
	return $name;
});

Result:

root@git:~# curl -X POST "https://api.domain.com/user/create" -H "X-Auth-Key: Bearer         8fbb80d7f378f2276246388093439422" --data '{"usname":"example"}'
<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'><title>Slim Application     Error</title>
</head><body><h1>Slim Application Error</h1>

<p>The application could not run because of the following error:</p><h2>Details</h2><div><strong>Type:    </strong> 

Error</div><div><strong>Message:</strong> 

Call to a member function getBody() on null</div><div><strong>File:</strong> 

/home/domain/public_html/_SubDomains/Api/index.php</div><div><strong>Line:</strong> 113</div><h2>Trace</h2>

<pre>#0 [internal function]: Closure-&gt;{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)


#1 /home/domain/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(41): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 /home/domain/vendor/slim/slim/Slim/Route.php(335): Slim\Handlers\Strategies\RequestResponse-&gt;__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 /home/domain/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\Route-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#4 /home/domain/vendor/slim/slim/Slim/Route.php(313): Slim\Route-&gt;callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 /home/domain/vendor/slim/slim/Slim/App.php(513): Slim\Route-&gt;run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 /home/domain/vendor/dyorg/slim-token-authentication/src/TokenAuthentication.php(74): Slim\App-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 [internal function]: Slim\Middleware\TokenAuthentication-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#8 /home/domain/vendor/slim/slim/Slim/DeferredCallable.php(43): call_user_func_array(Object(Slim\Middleware\TokenAuthentication), Array)
#9 [internal function]: Slim\DeferredCallable-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#10 /home/domain/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#11 /home/domain/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\App-&gt;Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 /home/domain/vendor/slim/slim/Slim/App.php(406): Slim\App-&gt;callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#13 /home/domain/vendor/slim/slim/Slim/App.php(314): Slim\App-&gt;process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#14 /home/domain/public_html/_SubDomains/Api/index.php(121): Slim\App-&gt;run()
#15 {main}</pre></body></html>

Why am I getting this error?
Thank you


#2

The error says Call to a member function getBody() on null, as you’ve passed $request to your function, but not $app (which isn’t needed).

Try

$app->post(’/user/create’, function ($request, $response, $args) {
    $json = $request->getBody();

#3

Hi @tflight

You’re great! Thank you.