We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Pb with tutorial REST

I followed this tutorial https://docs.phalcon.io/en/latest/reference/tutorial-rest.html,

I copy paste exactly the same code. I succeed to get the list of robots I have installed Xamp PHP 5.6

But in i try to add a robot using curl i have the followgin error message: curl.exe -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}' https://localhost/sample/api/robots <br /> <b>Notice</b>: Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>118</b><br /> <br /> <b>Notice</b>: Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>119</b><br /> <br /> <b>Notice</b>: Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>120</b><br /> <br /> <b>Fatal error</b>: Class 'Response' not found in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>124</b><br />

And when I try to update an existing robot, i have the followgin error message: curl.exe -i -X PUT -d '{"name":"ASIMO","type":"humanoid","year":2000}' https://localhost/sample/api/robots/2 <br /> <b>Notice</b>: Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>173</b><br /> <br /> <b>Notice</b>: Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>174</b><br /> <br /> <b>Notice</b>: Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>175</b><br /> <br /> <b>Fatal error</b>: Class 'Response' not found in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>179</b><br />

Can you help me please?



85.5k

give us what's in D:\Sources\xampp\htdocs\mailman\index.php lines: 118,119,120,124,173,174,175,179

edited Mar '16

The lines given errors:

$status = $app->modelsManager->executeQuery($phql, array(
    'name' => $robot->name,
    'type' => $robot->type,
    'year' => $robot->year
));

// Create a response
$response = new Response();

The complete function Add:

// Adds a new robot
$app->post('/api/robots', function() use ($app) {

$robot = $app->request->getJsonRawBody();

$phql = "INSERT INTO Robots (name, type, year) VALUES (:name:, :type:, :year:)";

$status = $app->modelsManager->executeQuery($phql, array(
    'name' => $robot->name,
    'type' => $robot->type,
    'year' => $robot->year
));

// Create a response
$response = new Response();

// Check if the insertion was successful
if ($status->success() == true) {

    // Change the HTTP status
    $response->setStatusCode(201, "Created");

    $robot->id = $status->getModel()->id;

    $response->setJsonContent(
        array(
            'status' => 'OK',
            'data'   => $robot
        )
    );

} else {

    // Change the HTTP status
    $response->setStatusCode(409, "Conflict");

    // Send errors to the client
    $errors = array();
    foreach ($status->getMessages() as $message) {
        $errors[] = $message->getMessage();
    }

    $response->setJsonContent(
        array(
            'status'   => 'ERROR',
            'messages' => $errors
        )
    );
}

return $response;
});


85.5k
edited Mar '16

did you register the service ?


 $di->set('modelsManager', function() {
      return new ModelsManager();
 });

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Manager.html

edited Mar '16

I didnt register the service, it seems already included.

When i call the get list of robots, it works:

// Searches for robots with $name in their name
$app->get('/api/robots/search/{name}', function ($name) use ($app) {
  $phql = " SELECT *
        FROM Robots
        WHERE name LIKE :name:
        ORDER BY name";
  $robots = $app->modelsManager->executeQuery(
  $phql,
  array(
      'name' => '%' . $name . '%'
  )
  );

  $data = array();
  foreach ($robots as $robot) {
  $data[] = array(
      'id'   => $robot->id,
      'name' => $robot->name
  );
  }

  echo json_encode($data);
});

So the problem not come from this part.

I succedeed to remove an error adding

use Phalcon\Http\Response;

now the error is:

<br />
<b>Notice</b>:  Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>161</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>162</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>D:\Sources\xampp\htdocs\mailman\index.php</b> on line <b>163</b><br />
{"status":"ERROR","messages":["name is required","type is required","year is required"]}

line 156 to 164

$robot = $app->request->getJsonRawBody();
//return print_r($app->request->getJsonRawBody());
$phql = "INSERT INTO Robots (name, type, year) VALUES (:name:, :type:, :year:)";

$status = $app->modelsManager->executeQuery($phql, array(
    'name' => $robot->name,
    'type' => $robot->type,
    'year' => $robot->year
));

when i do this:

return print_r($app->request->getJsonRawBody());

the result is empty.



85.5k
edited Mar '16

some expert has to join here, but i think you need to send proper object to this, my guess is you need to call this with params like :

{ "name": "robot name" , "type" : "metal" , year : "2222" }

or even maybe wrapped in

{ robot: { "name": "robot name" , "type" : "metal" , year : "2222" } }

but i dont know someone else has to help you if i am wrong here

edited Mar '16

Thank you for responding :)

I found something interesting there:

https://forum.phalcon.io/discussion/4162/handling-request-with-applicationjson-contenttype-header

The problem comes from the Content Type, and when i do this:

$app->request->getRawBody()

Instead of this:

$app->request->getJsonRawBody()

I can get my result, so the problem is on the curl call:

curl.exe -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}'    https://localhost/test/api/robots

I will try to find a way to modify the Content Type, the tutorial need to be updated, do you know who to contact in this kind of situation?

edited Mar '16

Even with :

curl.exe -i -X POST -d '[{"name":"C-3PO","type":"droid","year":1977}]' -H "Content-Type: application/json"   https://localhost/test/api/robots

Or

curl.exe -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}' -H "Content-Type: application/json"   https://localhost/test/api/robots

I continue to have the same problem

$app->request->getJsonRawBody()

Is still empty.