Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pete_cen's avatar

In server, Laravel returns 200 on validation failure instead of 422

Hi. I'm having an issue with a Laravel API I've developed. I'll try to give you all info as I can.

In local, when calling the API with a request wich have to fail, it works fine. The response has a 422 code and the message is correct. However, calling the same endpoint in the server returns a 200 code but with the correct error message. The validation works, but the code is not correct. This is happening in Postman and in the app that's calling the API. And in two different computers. The code in local and in the server is the same. I'm working in local in the same branch that's deployed.

Laravel version

Laravel 10.10 (from composer.json: "laravel/framework": "^10.10").

Form Request

public function rules(): array
    {
        return [
            'currentPassword' => 'required|string',
            'newPassword' => 'required|string|min:8'
        ];
    }

I'm not posting the controller function beacause is not even reaching it, as it should be since it's a validation error what I'm testing.

Request Body

{
    "currentPassword": "pass",
    "newPassword": "12345"
}

Request Headers

Content-Type: application/json
Accept: application/json
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

There are some more but are Postman-related and the bearer token. The headers are the same in both requests. I duplicated the Postman tab and checked it several times. The only change in headers is in the bearer token.

Response Headers

Here is where there are differences. From my local:

Content-Type: application/json
X-Powered-By: PHP/8.2.7

From the server:

Content-Type: text/html; charset=UTF-8
X-Powered-By: PHP/8.1.3

The Content-Type headers are different in the response headers, but I don't know why. I don't think that it is related with the PHP version. I've looking around and I've seen posts with the same problem but they solved it with the Content-Type headers in the request, but I already have it and it doesn't work.

.env

In local:

APP_ENV=local
APP_DEBUG=true

In server:

APP_ENV=production
APP_DEBUG=false

I've changed the APP_DEBUG to true but same result.

I don't know what's happening and I don't know what I can do. Any help is appreciate. Thanks.

Edit In the server, the API is running on an Docker container. I recreated the container in local and is giving the same problem. 200 instead of 422 code. So, it seems to be something about the container. The base image is: php:8.1.3-apache

0 likes
0 replies

Please or to participate in this conversation.