PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 13, 2022

[FIXED] cakephp3- not able to fetch authorization header inside controller

 February 13, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

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


Solution

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');


Answered By - chrisShick
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing