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
request()function but with this config it is ok, I removed the scoped clients.