Welcome Guest, Not a member yet? Register   Sign In
Cannot access request headers in controller
#1
Question 

Hello. I am just having a hard time working with request headers. I did a custom Controller which extends CodeIgniter\Controller. In the constructor (public function initController(\CodeIgniter\HTTP\RequestInterface $request,.....)  I try using the function $request->getHeader('any-header'); and they always return an empty object. Am I doing something wrong? Using the function $request->headers(); crashes because of error "Call to undefined method", even using $this->request->getHeader("any-header"); 
As a workaround I used php function getallheaders(). I'd like using the codeigniter functions. Any clue to fix this?
Reply
#2

Can you please show your code so that we can help you.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Hello. Thank you. I am pasting the code. There is a line that says "//THIS LINE"

If I send a request without a "Service-ID" header, I get a null in "$this->o->header1". If I send the header with some random text, like "aaa", "$this->o->header1" turns into an empty object.

--------------------------------------------
<?php
namespace App\Controllers;

use App\Models\UsersModel;
use App\Libraries\CommonUser;
use CodeIgniter\Controller;

class ExtAppBaseController extends Controller
{

protected $helpers = [];
protected $me;
protected $o;

/**
* Constructor.
*/
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);

$this->o = new \stdClass;
$this->o->code = 100;
$this->o->header1 = $this->request->getHeader("Service-ID"); // THIS LINE

}
}

-----------------
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use Config\Services;

class AppExt1Api extends ExtAppBaseController
{
public function processGet($section1) {
header('Content-Type: application/json');
echo json_encode( $this->o );
}
}
Reply
#4

The getHeader() is not yet compatible with PSR-7's.
It returns a single Header object or an array of Header objects.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB