Failing to get request parameter values

Hi

I am trying out the rest apis given in Tutorial 3.

However, when I am working with Insert and update functions, the post parameters don't get recognized in my code.

```php $app->post('/api/robots', function() use ($app) {

$robot = json_decode($app->request->getRawBody());

// rest of my code

} ```

My $robot value is null and hence I am not able to process rest of the parts of the function.

Any idea where I might be going wrong? Any help is appreciated.

Thanks!



82.2k

Hi, the tutorial uses CURL to test the application, the POST data is passed as a JSON string in the raw http body:

curl -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}' http://localhost/my-rest-api/api/robots

If you're testing the application in a browser, you can change that line by:

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


2.0k

Thanks for your response. I was actually using curl to test, exactly the way described in the tutorial. But the json_decode still doesn't work for me.

Will try testing using the browser. May I know what request functions can be used to test out put and delete requests?

Thanks! Appreciate your help.



82.2k

PHP does not provide automatic superglobals when the request is made via PUT or DELETE, the framework allow you to read the HTTP Raw body using $app->request->getRawBody()

edited Jul '14

Hi, I have the same problem when testing (with curl, just like say the tutorial). I get errors with the POST and PUT methods.

When send this:

curl -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}' http://localhost/phalcon%5test/rest/robots

If print the input (immediately after receiving it) itshows that the receiving object is NULL.

//Adds a new robot $app->post('/robots', function() use ($app) { $robot = jsondecode($app->request->getRawBody()); echo jsonencode($robot); }

Some suggestion??

Thanks in advance, greetings

UPDATE: If I changed the line: $robot = json_decode($app->request->getRawBody()); to: $robot = $app->request->getPost(); get the same NULL output (only change the type of input, string to array)