Reputation: 1749
I have the route:
$router->group(['middleware' => 'auth'], function ($router){
$router->post('/create-update/', ['uses' => 'ReceiveUserLogin@createUpdate']);
});
which obviously leads to my RecieveUserLogin controller which ends with a response message method like this:
public function returnMessage($statusCode, $message)
{
if($statusCode >= 400) {
return abort($statusCode, $message);
} else {
$content = ['status' => $statusCode, 'message' => $message];
$response = new Response;
$response->setContent($content);
return $response;
}
}
but in postman I get a status code of 200 OK which is great but I want also have the json response info I put in there before. Any idea what I am doing wrong?
Upvotes: 1
Views: 1848
Reputation: 325
When creating your response you need to set it to json type. Either set a header that tells it the response type of json, or (like in the documentation) use the json helper function.
In your code above, change this:
$content = ['status' => $statusCode, 'message' => $message];
$response = new Response;
$response->setContent($content);
return $response;
to this:
$content = ['status' => $statusCode, 'message' => $message];
return response()->json($content);
Also, make sure to return the response directly from the controller method. The return goes up to the method that calls it, it doesn't exit or print immediately. So if you don't return from the controller, it won't return at all.
Upvotes: 2