0

I have a Laravel 8 API application. Below is my example test method in the controller which return status 422 with invalid data JSON response.

public function testInvalid()
    {
        $arrResponse = [
            'success'    => false,
            'message'    => 'message',
            'data'       => [],
            'errors'     => ['error'],
            'error_code' => 422,
        ];

        return response()->json($arrResponse, 422);
    }

the above should return the JSON response in postman:

{
    "success": false,
    "message": "message",
    "data": [],
    "errors": [
        "error"
    ],
    "error_code": 422
}

But it only works on the local machine.

When I run same on the server it returns the Unprocessable entity HTML page.

But if I update it to 200 OK response code with the same response format. then it returns JSON response from the server too.

        return response()->json($arrResponse, 200);

all other valid request works fine and returns JSON response from the server.

Is it something I need to check on Apache web-server configuration?

Note: I tried passing Accept: application/json and Content-Type: application/json headers in postman request. still the same.

Amit Shah
  • 7,771
  • 5
  • 39
  • 55
  • Turn on Debug mode and Check the error logs – Abdulla Nilam Apr 18 '23 at 06:29
  • The debug mode in laravel is ON on the server too. no logs in the laravel logs. As it not considered Exception. 422 is validation error (means valid response) but just not returning json, instead default HTML page. – Amit Shah Apr 18 '23 at 06:32
  • Can you verify that the response is really coming from Laravel and not from the web server? Like post a screenshot of the page or the HTML. I could imagine that the web server returns its own HTML instead of the real framework response. – Kevin Apr 18 '23 at 12:46
  • @Kevin yes the response page is of web server according to the error code generated by json response. if I set it to 404 then the default html not found page is thrown. and if 422 then the associated html page for it. so only the json body is missing. but the error codes are correctly thrown back to response. – Amit Shah Apr 19 '23 at 01:20
  • This is not a laravel issue. – marius-ciclistu Apr 23 '23 at 20:47
  • @marius-ciclistu yes I suspect too; may be apache issue. but if you know the best answer where and how to fix then please let me know. – Amit Shah Apr 24 '23 at 03:14
  • It could also be a custom middleware that throws exception on non 200 status codes in laravel. If it is not this case, then you should search a way to overwrite the php response for non 200 status codes and then do the opposite (server side). Also have a look here: https://laravel.com/docs/10.x/errors#ignoring-exceptions-by-type – marius-ciclistu Apr 24 '23 at 05:25
  • @marius-ciclistu that middleware should also act same on the local. but let me clear again that there is no such middleware in my case. it's very straight route. – Amit Shah Apr 25 '23 at 04:12
  • Then the issue is from the load balancer or ngnx/apache. – marius-ciclistu Apr 25 '23 at 05:03

1 Answers1

-1

This is due to the Accept header of your request.

If not provided, Laravel defaults to Accept: text/html and error handling will provide you with a response matching your accept header.

By providing Accept: application/json with your request headers, Laravel will return the error response as JSON as well.

Repox
  • 15,015
  • 8
  • 54
  • 79