Mark
Mark

Reputation: 3197

accessing json input with laravel

I am using restangular to post a request to users/login

my laravel route is like so

Route::post('users/login', array('as' => 'login', function () {
    $input = Input::all();
    return Response::json($input);
}));

the data in the post header is formatted like so

{"input":["username":"un","password":"pw","remember":false]}

this dosn't work either

{"username":"un","password":"pw","remember":false}

this route is returning an empty array []. I am guessing my input is formatted incorrectly as from the laravel docs

Note: Some JavaScript libraries such as Backbone may send input to the application as JSON. You may access this data via Input::get like normal.

edit: it is working with this input ie. no quotes

{username:un,password:pw,remember:false]}

Upvotes: 3

Views: 1170

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219938

This is what you're looking for:

$input = Request::json()->all();

You can test it by returning the array directly from your route:

Route::post('users/login', array('as' => 'login', function ()
{
    $input = Request::json()->all();

    return $input;
}));

Upvotes: 2

Related Questions