0

I am sending data in json format using postman to my server in Laravel 4.2,

{
 "id_servicio" = "8",
 "user_token" = "3853656261737469616e3130"
}  

to receive all infomation I use the Input::json()->all()method but this nothing recive and apper Error Undefined index: user_token error.

My code:

Route

Route::post('motoboy/service/finish/info', array('uses' => 'ServicioController@finishServicio'));

Controller

function finishServicio(){
    $input = Input::json()->all();

    $token = $input['user_token']; // Error in this part
    $idServicio = $input['id_servicio'];


    $servicio = DB::table('servicios')->where('id_servicio', $idServicio)->first();
    $motoboy = DB::table('motoboys')->where('auth_token', $token)->first();

    if($motoboys != null){
        if($servicio != null){

            $array = array('Code' => '202', 'Message' => 'EL servicio existe y está listo para recibir las imagenes');
            return Response::json($array);
        }else{
            $array = array('Error' => 'Servicio no encontrada',  'Code' => '404', 'Message' => 'Servicio no encontrado');
            return Response::json($array);
        }
    }else{
        $array = array('Error' => 'Motoboy no autentificado',  'Code' => '401', 'Message' => 'Motoboy no autentificado');
            return Response::json($array);
    }    
}

When I use dd($input) function, return this array{0} How can I fix it?


Edit

I am sending information from Android app to my server in laravel. I am using GSON to send the information in JSON Format, for this reason I need to use Input::json()->all() because receive the input in JSON, if I use Input::all() and I send a JSOn, that method doesnt receive nothing

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
  • check this http://stackoverflow.com/questions/23327169/decode-laravel-4-inputjson you don't need the all() method, and you can call the attributes like $input->get('user_token') – Flavio Jan 29 '15 at 20:24

1 Answers1

1

Change Input::json()->all(); to Input::all(); it should work.

  • works but I am sending the information in JSON Format, any way to transform it? – TuGordoBello Jan 29 '15 at 20:27
  • you like to transform it, to what exactly? – aminekaabachi Jan 29 '15 at 20:31
  • I am sending information from Android app to my server in laravel. I am using ``GSON``to send the information in JSON Format, for this reason I need to use ``Input::json()->all()`` because receive the input in JSON, if I use ``Input::all();`` and I send a JSOn, that method doesnt receive nothing – TuGordoBello Jan 29 '15 at 20:36
  • You need to make sure you are passing the `contentType: "application/json"` in the request, then laravel is smart enough to recognize it – aminekaabachi Jan 29 '15 at 20:46