1

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?

David Jarrin
  • 1,635
  • 4
  • 19
  • 40

1 Answers1

2

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.

Alec Gordon
  • 325
  • 4
  • 10
  • hmmm, yea I tried that, both the response()->json and the response($content)->header('content-type','application/json') methods but I am still just getting no json back in postman (nothing under the body tab) and oddly enough I am getting headers back of content-type text/html – David Jarrin Sep 13 '17 at 15:53
  • So no body is coming back at all? Have you tried to `dd` the content to ensure its going along that path? And your controller method is returning exactly what comes from the return message function? – Alec Gordon Sep 13 '17 at 15:58
  • k dd'ing the content is much closer now I get html back, when I try to view it as json I get the message of "Unexpected '<'" but I can see the data in the html – David Jarrin Sep 13 '17 at 16:04
  • If you just `echo $content;` right before creating your response, does it show in the body? It's hard to tell, but I think it may be the program flow? So just to make sure, you're POSTing from Postman to `'/create-update/'` which does `return returnMessage($statusCode, $message);` which is what builds the response? If your'e getting a __200__ but no content, it leads me to believe that your application is returning something other than the response. – Alec Gordon Sep 13 '17 at 16:10
  • Yea I get a long script tag returning (starts with – David Jarrin Sep 13 '17 at 16:20
  • 1
    k I figured it out (with your help), I had to return from that actual controller that the route was pointing to, not from the returnMessage method. – David Jarrin Sep 13 '17 at 16:33
  • @Alec Gordon Maybe you can help me. Look at this : https://stackoverflow.com/questions/52249289/why-post-request-from-postman-returns-empty – moses toh Sep 10 '18 at 01:05