# Wrong status and Content-Type in controller

**URL:** <https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927>\
**Category:** Questions\
**Created:** [June 26, 2024, 9:56am UTC](https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927 "2024-06-26T09:56:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArmaNx7](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ArmaNx7](https://discourse.slimframework.com/u/ArmaNx7)\
**Post date:** [June 26, 2024, 9:56am UTC](https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927/1 "2024-06-26T09:56:24Z")

</div>

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:

```auto
<?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

```auto
$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.

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [June 26, 2024, 10:08am UTC](https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927/2 "2024-06-26T10:08:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ArmaNx7](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ArmaNx7](https://discourse.slimframework.com/u/ArmaNx7)\
**Post date:** [June 26, 2024, 10:36am UTC](https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927/3 "2024-06-26T10:36:09Z")

</div>

here this is my index.php file and definitions:

```auto
<?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();
?>

```

```auto
<?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.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 26, 2024, 3:45pm UTC](https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927/4 "2024-06-26T15:45:40Z")

</div>

> [@ArmaNx7](#):
>
> `$app = Bridge::create($container);`

What is this Bridge class doing?

---

<div class="post-metadata">

**Author:** ![ArmaNx7](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@ArmaNx7](https://discourse.slimframework.com/u/ArmaNx7)\
**Post date:** [June 26, 2024, 5:44pm UTC](https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927/5 "2024-06-26T17:44:59Z")

</div>

Bridge Class:  
[PHP-DI bridge](https://php-di.org/doc/frameworks/slim.html)

I’ve replaced this part of code with

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

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

```

But still the same.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 27, 2024, 5:09pm UTC](https://discourse.slimframework.com/t/wrong-status-and-content-type-in-controller/5927/6 "2024-06-27T17:09:23Z")

</div>

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