I've been working on a Laravel API with validation. I'm sending this request to the server:
> POST /api/v1/clients HTTP/1.1
> Host: localhost
> User-Agent: insomnia/2022.3.0
> Content-Type: application/json
> Accept: application/json
> Authorization: Bearer ***************
> Content-Length: 16
{
"name": "",
}
On my development system, running windows/Apache/PHP 8.1, the POST requests return a JSON object containing validation errors.
{
"message": "The name field is required.",
"errors": {
"name": [
"The name field is required."
]
}
}
But my test system, running Linux/Apache/PHP 8.1, does not return a 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:
api.php
Route::group(['middleware' => 'auth:sanctum', 'prefix' => 'v1'], function () {
Route::apiResource('clients', ClientApiController::class);
});
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 code. 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. As one is Windows and the other Linux, it's not straight forward to check the differences.
Any reason I'm not getting a JSON response back from the test server?
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.
Server | PHP | Response body |
---|---|---|
Apache | mod-php | Included |
Apache | php-fpm | Missing |
Nginx | php-fpm | Included |
I found a few links with an issue with the request body being drop so may be the same thing or something similar.
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. Thanks for the help everyone.