cdgraham's avatar

Apache not return Laravel json validation errors on Test but works on Dev

I've been working on a Laravel 9 API with validation. On my dev system, using Windows/Apache/PHP 8.1, the POST requests return a 422 with the json validation errors. My request header includes "Accept: application/json".

{
    "message": "The name field is required.",
    "errors": {
        "name": [
            "The name field is required."
        ]
    }
}

I have deployed the code to the Test system, which uses Linux/Apache/PHP 8.1. When I run the exact same request, I just get a 422 error without the json response.

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>422 Unprocessable Content</title>
</head><body>
<h1>Unprocessable Content</h1>
<p>The server understands the media type of the
request entity, but was unable to process the
contained instructions.</p>
</body></html>

Here is my code but as it works on the my Dev system I can't think how it's wrong. StoreClientRequest.php

public function rules()
    {
        return [
            'name' => 'required|string|max:255'
        ];
    }

ClientAPIController.php

public function store(StoreClientRequest $request)
    {
        $client = Client::create($request->all())->fresh();

        return (new ClientResource($client))->response()->setStatusCode(201);
    }

Since it's working on my localhost I don't believe it is the Laravel code but I suppose stranger things have happened.

If I send the POST with the name filled in I do get a proper 201 response back and the record is created on both on the dev and the test system.

I'm guessing it's an issue with Apache or PHP configuration on the Test server but I'm not seeing anything different as far as I can tell. The only other difference is I'm using php-fpm on the test server as I have an app that is still on PHP 7.4 at the moment As one is Windows and the other Linux, it's not straight forward to check the differences.

apache site conf has

<Directory /var/www/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
</Directory>

Anyone run in to something like this? Am I missing something on the test server?

0 likes
2 replies
cdgraham's avatar
cdgraham
OP
Best Answer
Level 2

It does appear to be an issue with Apache dropping the body of the response when using php-fpm. I tested a couple of different configurations of Apache, Nginx, mod-php, and php-fpm.

  • Apache with mod-php the Response body is Included
  • Apache with php-fpm the Response body is Missing
  • Nginx with php-fpm the Response body is Included

I found a few links with an issue with the request body being drop so may be the same thing or something similar.

https://www.jeffgeerling.com/blog/2017/apache-fastcgi-proxyfcgi-and-empty-post-bodies-chunked-transfer

Looks like I will need to upgrade the other apps to PHP 8 so I can switch back to mod-php or move to nginx.

amitshahc's avatar

Same issue here. Working as expected on local machine with proper json response with 422 validation error. but not on the UAT/Test server. it returns HTML page of 422. May be this will help me to check. but need to wait if my senior will let me see into this on Test server.

Please or to participate in this conversation.