5

I would like to send a custom http response with status as 500. Tried the below code, but it's returning status as 200.

return response()
            ->json([
                'code'      =>  500,
                 'message'   =>  'custom error'
            ], 500);

How can I send http status 500 from laravel controller?

Abdunnasir K P
  • 117
  • 1
  • 2
  • 7
  • 1
    Possible duplicate of [Laravel - Return json along with http status code](https://stackoverflow.com/questions/31131159/laravel-return-json-along-with-http-status-code) – Script47 Nov 29 '18 at 11:37
  • i think u missing something here (your code is working fine) – Jignesh Joisar Nov 29 '18 at 11:44

1 Answers1

10

Try this.

return abort(500, 'custom error');

For more info:

https://laravel.com/docs/5.7/errors#http-exceptions

Hope this helps

Update

Try this then

return response()->json(['message' => 'error message'], 500);

That will send a 500 status code instead of a 200

For more info:

https://laravel.com/api/5.7/Illuminate/Contracts/Routing/ResponseFactory.html#method_json

Hope that helps.

Josh
  • 1,316
  • 10
  • 26