phppostmanhttp-headersslim-4

Slim4 - getHeader is returning empty


I am getting an empty array from slim4 $request->getHeader('accept') method to do checksum authorization. Also $request->getParsedBody() is working fine. I followed the code from documentation like below:

<?php

declare(strict_types=1);

namespace App\Application\Actions\Users;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Slim\Psr7\Response;
use Cake\Validation\Validator;
use Selective\Validation\ValidationResult;
use Selective\Validation\Factory\CakeValidationFactory;
use Selective\Validation\Exception\ValidationException;
use PDO;
use \Firebase\JWT\JWT;
use App\Application\Helpers\CommonHelper;

class ChangePwdAction implements RequestHandlerInterface
{
    private $logger;
    private $connection;

    public function __construct(PDO $connection,LoggerInterface $logger)
    {
        $this->connection = $connection;
        $this->logger = $logger;
    }

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        echo $request->getHeader('userId');exit;
    }
}

API Call:

<?php
declare(strict_types=1);

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Application\Actions\Users\ListUserAction;
use App\Application\Actions\Users\ChangePwdAction;
$app->group('/v1/user', function (Group $group) {
        $group->get('', ListUserAction::class);
        $group->post('/changePwd/{id}', ChangePwdAction::class);
    });

Postman request call

LOG Results and empty array: LOG Result


Solution

  • you need to use $request->getHeader('userId')[0]