Symfony Http Client default headers not added

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:

        base_uri: '%env(the_url)%'
          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

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *