'Laravel - POST data is null when using external request

I'm new to laravel, and I'm trying to implement a simple rest api.

I have the controller implemented, and tested via unit testing.

My problem is with the POST request.

Via the tests Input:json has data, via an external rest client it returns null.

This is the code on the unit test

    $newMenu = array(
      'name'=>'Christmas Menu', 
      'description'=>'Christmas Menu',
      'img_url'=>'http://www.example.com',
      'type_id'=>1,
    );
    Request::setMethod('POST'); 
    Input::$json = $newMenu;
    $response = Controller::call('menu@index');

What am I doing wrong?

UPDATE:

This is realy driving me crazy

I've instanciated a new laravel project and just have this code:

Routes

Route::get('test', 'home@index');
Route::post('test', 'home@index');

Controller:

class Home_Controller extends Base_Controller {

    public $restful = true;
    public function get_index()
    {
        return Response::json(['test'=>'hello world']);
    }
    public function post_index()
    {
        return Response::json(['test'=>Input::all()]);
    }
}

CURL call:

curl -H "Accept:application/json" -H"Content-type: application/json" -X POST -d '{"title":"world"}' http://localhost/laravel-post/public/test

response:

{"test":[]}

Can anyone point me to what is wrong.

This is really preventing me to use laravel, and I really liked the concept.



Solution 1:[1]

Because you are posting JSON as your HTTP body you don't get it with Input::all(); You should use:

$postInput = file_get_contents('php://input');
$data = json_decode($postInput, true);

$response = array('test' => $data);
return Response::json($response);

Also you can use

Route::any('test', 'home@index');

instead of

Route::get('test', 'home@index');
Route::post('test', 'home@index');

Solution 2:[2]

If you use : Route::post('test', 'XYZController@test');
Send data format : Content-type : application/json
For example : {"data":"foo bar"}

And you can get the post (any others:get, put...etc) data with :

Input::get('data');

This is clearly written in here : http://laravel.com/docs/requests . Correct Content-type is very important!

I am not sure your CURL call is correct. Maybe this can be helpful : How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

I am using Input::get('data') and it works.

Solution 3:[3]

Remove header Content-type: application/json if you are sending it as key value pairs and not a json

Solution 4:[4]

I was facing this problem, my response of post was always null. To solve that I put the body key in guzzle object, like this

$client = new Client([
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => config('app.callisto_token'),
        ]
    ]);

    $body = [
            'firstResult'=> 0,
            'data' => '05/05/2022'
        ];

        $response = $client->post('http://'.$this->ip.'/IntegracaoERP'.'/status_pedido',
            ['body' => json_encode($body)]
        );

Don't forget the json_encode in body key. Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marius Kažemėkaitis
Solution 2 Community
Solution 3 Rayiez
Solution 4 Jonathan Güths