Wrong status and Content-Type in controller

Hi I have a problem with status codes and header content type.
Status is always 200 and content is always text/html This my routes.php

$app->post('/', [UserController::class, 'login']);

And this my Controller:

<?php
    declare(strict_types = 1);

    namespace App\Controllers\User;

    use App\Services\User\UserService;
    use App\Controllers\BaseController;
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;

    final class UserController extends BaseController {
        private $userService;

        public function __construct(UserService $userService)
        {
            $this->userService = $userService;
        }

        public function login(Request $request, Response $response): Response {
            $response_str = json_encode(['message' => 'Welcome to our API']);
            $response->getBody()->write($response_str);
            return $response->withStatus(401)
                            ->withHeader('Content-Type', 'application/json');
        }
}

?>

When I call this method my response is always 200, and content type is text/html

But if i move my code into routes.php directly, something like below it works correctly

$app->post('/', function(Request $request, Response $response) {
    $response_str = json_encode(['message' => 'Welcome to our API']);
    $response->getBody()->write($response_str);
    return $response->withHeader('Content-Type', 'application/json');
});

I have this similar problem with my custom error handling.

You should use a step debugger to step through and see what’s happening as we need more info.

here this is my index.php file and definitions:

<?php
    declare(strict_types=1);

    use DI\Container;
    use DI\ContainerBuilder;
    use DI\Bridge\Slim\Bridge;
    use App\Handler\ApiError;

    require_once __DIR__ . '/../vendor/autoload.php';

    $containerBuilder = new ContainerBuilder();
    $containerBuilder->addDefinitions(__DIR__ . '/../src/config/definitions.php');
    $container = $containerBuilder->build();

    $app = Bridge::create($container);

    //Parsing Middleware
    $app->addBodyParsingMiddleware();
    //Routing Middleware
    $app->addRoutingMiddleware();
    //Error Middleware
    //$myErrorHandler = new ApiError($app->getCallableResolver(), $app->getResponseFactory());

    $errorSettings = $container->get('settings')->getErrorSettings();
    $errorMiddleware = $app->addErrorMiddleware(
        $errorSettings['displayErrorDetails'], 
        $errorSettings['logErrors'], 
        $errorSettings['logErrorDetails']
    );

    //$errorMiddleware->setDefaultErrorHandler($myErrorHandler);
    //Routes
    require_once __DIR__ . '/../src/routes/api.php';

    $app->run();
?>
<?php
    use App\Config\Settings;
    use App\Config\DB;
    use function DI\create;

    return [
      'database' => create(DB::class),
      'settings' => create(Settings::class)
    ];
?>

I don’t have complicated code and I’m at the beginning, that’s all I do. I hope this helps you understand why this is happening.

What is this Bridge class doing?

Bridge Class:
PHP-DI bridge

I’ve replaced this part of code with

    $containerBuilder = new ContainerBuilder();
    $containerBuilder->addDefinitions(__DIR__ . '/../src/config/definitions.php');
    $container = $containerBuilder->build();

    AppFactory::setContainer($container);
    $app = AppFactory::create();

But still the same.

Why do you return a 401 Unauthorized error in your controller class?