6

I'm using the Http Client to send a request to an external API. According to the documentation I can add default headers to a specific route like so:

framework:
  http_client:
    scoped_clients:
      the_api:
        base_uri: '%env(the_url)%'
        headers:
          Authorization: 'Bearer %env(my_token)%'

That doesn't work for me. I also tried this option:

auth_bearer: '%env(my_token)%'

same effect.

In the service I use to make the request I have:

$response = $client->request('POST', $theUrl, [
    'body' => $dataArray
]);

With the above configuration I get a 403 response, as if the token is not sent. If I add it to the request() function however it works:

$response = $client->request('POST', $theUrl, [
    'body' => $dataArray,
    'headers' => [
        'Authorization' => 'Bearer ' . $myToken
    ]
]);

I'm looking for a way to avoid fetching the token in my service or controller. In the future I might send other requests to the API (or a different API) and I don't want to set the token for each one.

If it matters $theUrl is https. Symfony and Http Client versions: 4.4

2
  • 1
    you may have overread this part, since you didn't mention it all: "If you use scoped clients in the Symfony framework, you must use any of the methods defined by Symfony to choose a specific service." (source: your reference) Commented Jul 8, 2020 at 8:35
  • @Jakumi you pointed me to something that works. I added the token as an argument when registering a service (I did't register it before). However instead of a service I changed it to a component which extends an abstract base class. If I have to make the same type of request to a different API will create another similar component. This works because different APIs might require different arguments. Thanks. PS I'm still adding it in the request() function but with this config it is ok, I removed the scoped clients. Commented Jul 8, 2020 at 9:11

2 Answers 2

12

You must use the client name the_api in your case.

#config/packages/framework.yaml
framework:
    http_client:
        scoped_clients:
            name_client:
                base_uri: 'http://ip:port/'
                auth_basic: 'user:password'

client name here name_client

#src/Controller/MyController.php
...
public function __construct(HttpClientInterface $name_client)
{
    $this->client = $name_client;
}
...

$name_client will contain an instance of the ScopingHttpClient class with the specified parameters

Enjoy!

Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar problem, my scoped client doesn't add default authorization header. My solution was to inject the token in my service and initialize a brand new flat HttpClient object like this:

$client = \Symfony\Component\HttpClient\HttpClient::create(['headers' => [
        'Authorization' => 'Bearer '. $this->token,
    ]]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.