Fixed Can't set default headers for Guzzle when calling XF\SubContainer\Http#createClient

Affected version
2.1.3

Kocal

New member
Hi :)

I'm facing an issue when trying to create a new Guzzle HTTP client with default headers.

PHP:
<?php
// config.php

$config['container']['my-api-client'] = function (\XF\Container $c) {
    return $c['http']->createClient([
        'base_uri' => 'http://example.com',
        'headers'  => [
            'Authorization' => 'the api key',
        ],
    ]);
};
Then when using the client, I found that header Authorization was not present inside the final configuration.
I've put some dump() and this is what happens:
Capture d’écran de 2019-09-05 08-40-01.png
2019-09-05_08-39.png


After some investigation, the issue is coming from the method applyDefaultClientOptions at line $options['headers'] = ['User-Agent' => 'XenForo/2.x (' . $xfOptions->boardUrl . ')'];, which simply define a new headers array over an existing one.

As a workaround, I modified the line with the following code and it works as expected:
PHP:
<?php
$options['headers'] = array_merge(
    ['User-Agent' => 'XenForo/2.x (' . $xfOptions->boardUrl . ')'],
    isset($options['headers']) && is_array($options['headers']) ? $options['headers'] : []
);
Thanks!
 

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue. It has now been resolved and we are aiming to include it in a future XF release (2.1.4).

Change log:
When setting http client defaults, do not overwrite existing headers that have been passed in.
Any changes made as a result of this issue being resolved may not be rolled out here until later.
 
Top