1

I'm performing and API that receive json object, but cannot read it. Actually I don't know if it should be received as Request, or how...

First I try to get the json and decode it to an array. After that, I return the same array to json in the response. But it's returned as empty ({})

That's my route

Route::post('enviar_encuestas', 'EncuestaUsuarioController@store');

And in my controller, for that method I perform the following

public function store(Request $request){
    $users = json_decode($request);
    return response()->json($users);
}

In Postman I send perform the following HTTP POST Request to

https://encuestasapi.informixsys.com.ar/public/enviar_encuestas

I select Body and raw, after that I select JSON(application/json) and write the following

{
        "id_encuesta" : 1,
        "email" : "[email protected]",
        "razon_social" : "asd",
        "nro_ref_autopack" : 1
}

1 Answer 1

4

You could retrieve the posted json data and return it back as json through:

public function store(Request $request){
    $users = json_decode($request->json()->all());
    return response()->json($users);
}
Sign up to request clarification or add additional context in comments.

2 Comments

$request->all() returns an associative array, no need to do json_decode given that you the $request is an instance of Illuminate\Http\Request. To return you can just do return response()->json($request->all());
Also, to decode JSON to an array, use the true flag json_decode($json, true); to return an associative array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.