Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
cakephp3- not able to fetch authorization header inside controller

I am developing an API using CakePHP 3 framework. Now I'm sending a GET request from POSTMAN client. User will pass an API key in the header. enter image description here

I wanna fetch this header in my controller function.

This is how my controller looks like

namespace Api\Controller;
use Cake\Auth\DefaultPasswordHasher;
use Api\Controller\AppController;
use Cake\Cache\Cache;
use Cake\Http\ServerRequest;

class ApiController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    }

    public function myinfo()
    {
        if($this->request->is('get')) { 
            $key = $this->request->getHeaderLine('Authorization');
            $this->set('key', $key);
        }
        $this->set('_serialize', ['key']);          
    } 
}

The error that I'm getting is: HeaderLine is not a function

I also tried some more options:

$acceptHeader = $this->request->getHeader('Authorization');

but this also threw similar error. Header is not a function.

Reference: Link

CakePHP version: 3.3.5

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As @ndm said in the OP comments, the last example in the linked doc should solve your problem. You are using a version prior to 3.4 so you have to use:

// Prior to 3.4.0
$key = $this->request->header('Authorization');
about 4 years ago · Santiago Trujillo Relatório

0

Refere document for Reading HTTP Header

Here mentioned that "Allows you to access any of the HTTP_* headers that were used for the request". Means it reads only http headers like

  • HTTP Request
  • Host
  • Connection
  • Upgrade-Insecure-Requests
  • User-Agent
  • Accept
  • Accept-Encoding
  • Accept-Language

Also mentioned that "While some apache installs don’t make the Authorization header accessible, CakePHP will make it available through apache specific methods as required."

So For Solution

They all have different obscure settings you can tweak to overrule this behaviour, but you'll need to determine exactly which module is to blame.

You can work around this issue by passing the header directly to PHP via the env:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

Also Refere: Zend Server Windows - Authorization header is not passed to PHP script

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda