guzzle: Authorization Bearer doesn't work

Hi there,

I try to get a request with Authorization bearer but it doesn’t work. Have a look to my code:

// in $this->endpoint I have https://172.17.8.111:443
$this->httpClient = new HttpClient( [ "base_uri" => $this->endpoint ] );

// prepare headers
$this->headers = [
      'verify'          => false,
      'allow_redirects' => true,
      'headers'         => [
        'Accept'        => 'application/json',
        'Authorization' => 'Bearer {$myToken}'
      ]
    ];

$this->response = $this->httpClient->get( '/api/', $this->headers );

I got [GuzzleHttp\Exception\ClientException] Client error: 401

the same request made via curl (in shell or php code) works!

What I wrong?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 2
  • Comments: 20 (4 by maintainers)

Most upvoted comments

@gfazioli because:

$var = ‘1234’;

echo “Value: $var”; // Value: 1234 echo ‘Value: $var’; // Value: $var

If your still having this issue, it might be related to your token which isn’t being parsed when using single quotes:

'Authorization' => 'Bearer {$myToken}' should be 'Authorization' => "Bearer {$myToken}"

I suspect that the problem lays in undefined port or protocol. Headers content wasn’t an issue for me. I resolved same problem by changing:

115.12.3.44 to 115.12.3.44:80

also tested

115.12.3.44 to http://115.12.3.44

or

google.com to http://google.com

Translated by @ImChriz 😉))

Can we reopen this. I think this issue persists when there is a redirect in place. When I am locally communicating between two services over local host using guzzle, everything runs fine, but if I deploy destination service on kubernetes, it returns 401, and the logs on the destination service suggests that ‘Authorization’ header was not received by it. Even the headers from the Response object in the exception suggest that there is no auth header, even though it is present in the Request object. Here is the snapshot of request and response: image