0
votes

I am trying to make a test work. I use a REST client, "Postman" to send a POST request with an object to Laravel. The idea is to get back the request as an object. But I don't think I am getting the right result.

In Postman, I specify the adress to send to, choose "POST", and then send this:

{
   "url": "http://testingtesting.com"
}

In Laravel I have a Route to the adress:

Route::post('api/test', 'TestController@index');

and the controller should return the json object. If I do this:

    class TestController extends BaseController{
       public function index(){
           $input = Input::json()->all();
           var_dump($input);
           }
    }

I get back an array in POSTman:

array (size=1)
  'url' => string 'http://testingtesting.com' (length=18)

That's when I look at "preview". If I switch to "Pretty" it shows like this:

<pre class='xdebug-var-dump' dir='ltr'>
    <b>array</b>
    <i>(size=1)</i>
  'url' 
    <font color='#888a85'>=&gt;</font>
    <small>string</small>
    <font color='#cc0000'>'http://testing.com'</font>
    <i>(length=18)</i>
</pre>

but I'm supposed to get an object(stdClass), not an array. What do I need to do?

1

1 Answers

0
votes

I got some info here (laracasts forum)get-raw-post-data. It seems to work with:

class TestController extends BaseController{
       public function index(){
           $data = json_decode(file_get_contents("php://input"));
           var_dump($data);

In Postman, that results in:

object(stdClass)[144]
  public 'url' => string 'http://testingtesting.com' (length=18)

But not sure if this is a recommended way, or why/how it works. I guess I have to read more about it. But if anyone wants to add any more info, or solutions, feel free!