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

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?



Solution 1:[1]

Merry Christmas and Welcome to Stackoverflow!

Firstly, an HTTP error code of 400 error means one or more settings is wrong at the client side. You can learn more about it here.

You seem to be missing out important headers configuration. You need to set the content-type header correctly otherwise the destination server won't be able to process your data.

Also, as a bonus point. You need to format your payload into a valid JSON string before sending out the request too.

import requests
import json

# Setup proper headers
headers = {
    "accept": "application/json, text/plain, */*",
    "content-type": "application/json; charset=UTF-8"
}

# Your body data here
payload = {"password":"password","username":"username","options":{"warnBeforePasswordExpired": True,"multiOptionalFactorEnroll": True}}
payload_json = json.dumps(payload) # Format it into a valid JSON str


with requests.Session() as s:
    p = s.post('https://sso.johndeere.com/api/v1/authn', headers=headers, data=payload_json)

    r = s.get("requested_url")
    
    print(p.content)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1