Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: getHeaders returns Array with no values #3616

Closed
milaan-muc opened this issue Sep 7, 2020 · 1 comment
Closed

Bug: getHeaders returns Array with no values #3616

milaan-muc opened this issue Sep 7, 2020 · 1 comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@milaan-muc
Copy link

milaan-muc commented Sep 7, 2020

Hi!

Yesterday i started a new project with CI4 and tried to implement a webhook with GitLab where i get a post to my CI installation when the repository has changes.

My problem is that the $request->getHeaders() function is returning an Array and i can see the fields but they are all empty.
The native php function getallheaders() returns everything.

Here is the debug output. First the CI function, second line the php function.

DEBUG - 2020-09-07 19:48:00 --> {"Content-Type":{},"Content-Length":{},"Host":{},"Connection":{},"X-Gitlab-Token":{},"X-Gitlab-Event":{}}
DEBUG - 2020-09-07 19:48:00 --> {"Content-Length":"1774","Host":"domain.local","Connection":"close","X-Gitlab-Token":"1234567890","X-Gitlab-Event":"Push Hook","Content-Type":"application\/json"}

My Controller:

<?php namespace App\Controllers;
use CodeIgniter\HTTP\Header;
class Home extends BaseController
{
public function git()
	{
		shell_exec('cd /srv/domain.local/ && git reset --hard HEAD && git pull && git pull');
		$header_ci = json_encode($this->request->getHeaders());
		$header_native = json_encode(getallheaders());
		log_message('debug', $header_ci);
		log_message('debug', $header_native);
	}
}
@milaan-muc milaan-muc added the bug Verified issues on the current code behavior or pull requests that will fix them label Sep 7, 2020
@michalsn
Copy link
Member

The thing is that getHeaders() method returns an array of Header objects. So it can't be converted just like that. You need to use something like:

$headers = array_map(function($header) { 
	return $header->getValueLine(); 
}, $this->request->getHeaders());

This way you gonna get the key => string value array of headers.

Maybe it's a good idea to prepare an additional method, like getHeadersArray() to make that easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

2 participants