8p / EightPointsGuzzleBundle

⛽️ Integrates Guzzle 6.x, a PHP HTTP Client, into Symfony
MIT License
440 stars 71 forks source link

Headers set in config.yml not overridden by passed options #49

Closed Coffee2CodeNL closed 7 years ago

Coffee2CodeNL commented 8 years ago

I've set the header in config.yml as following, using anonymous OAuth authentication

guzzle:
    clients:
        imgur_api:
            base_url: "https://api.imgur.com/3/"
            headers:
                Accept: "application/json"
                Authorization: "Client-ID %imgur_client_id%"

Later, when a user is authenticated, i replace the header in the code:

$parameters = [
    "json" => [
        "image" => $image,
        "type" => "base64",
    ],
    "headers" => [
        "Authorization" => "Bearer {$userEntity->getAccessToken()}"
    ],
];

And when i check on the logs, i find this:

* Hostname in DNS cache was stale, zapped
*   Trying 185.31.19.193...
* Connected to api.imgur.com (185.31.19.193) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*    subject: C=US; ST=California; L=San Francisco; O=Imgur, Inc.; CN=*.imgur.com
*    start date: 2013-10-22 12:00:00 GMT
*    expire date: 2016-11-10 12:00:00 GMT
*    subjectAltName: api.imgur.com matched
*    issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
*    SSL certificate verify ok.
> POST /3/image HTTP/1.1
Host: api.imgur.com
User-Agent: GuzzleHttp/6.2.0 curl/7.43.0 PHP/7.0.7-4+deb.sury.org~wily+1
Content-Type: application/json
Accept: application/json
Authorization: Client-ID XXX
Content-Length: 67534

As you can see, the Authorization header is not being replaced :(

What's going wrong here?

florianpreusner commented 7 years ago

I tested this issue with the current v5.2.1 (https://github.com/8p/GuzzleBundle/releases/tag/v5.2.1) without any problems. So this seems to be fixed.

I used following code: config.yml:

guzzle:
    clients:
        httpbin:
            base_url: "https://httpbin.org"
            headers:
                Accept: 'application/json'

Controller:

$client = $this->get('guzzle.client.httpbin');
$response = $client->get('/get'); // using headers by config
$body = $response->getBody()->getContents();

// => request header: Accept: 'application/json'

$options = [
    "headers" => [
        "Accept" => "text/html"
    ],
];
$response = $client->get('/ip', $options);

// => request header: Accept: 'text/html'