Skip to content

[7.x] Http Client adds multiple Authorization tokens #31848

@darki73

Description

@darki73
  • Laravel Version: 7.0.8
  • PHP Version: 7.4

Description:

Whenever you are trying to use the ->withToken('token-here') method, instead of replacing existing token, the Authorization header is now changed to array instead of string and the are now 2 tokens in there.

Steps To Reproduce:

// $client - instance of PendingRequest
$client = Illuminate\Support\Facades\Http::withHeaders([
    'User-Agent'    =>  'Not the user agent you were looking for'
])->withToken('SuperAmazingTokenForOneTypeOfOperations', 'Bearer');

// Do something which requires the original token

// And here is the problem, Authorization header is not 
// a string anymore but rather an array containing two tokens
$client->withToken('ThisIsNEWTokenForDifferentOperation', 'Bearer')->post();

You might think that the following piece of code will overwrite (i know it won't and i am sure you knew that already) the Authorization header if called explicitly:

$client->withHeaders(['Authorization' => 'Bearer ThisIsNEWTokenForDifferentOperation'])

It simply appends new token to the now array (i think because of the array_merge_recursive function call inside the withHeaders method)

Use Case:

I have a wrapper (Client class) for the api service, since most of the times the configuration is the same across all of the routes of the api, i've made a class which simply returns me the PendingRequest class instance, so i can call different methods without recreation of the Http 'helper'.

Suggestions:

  1. Replace old token with new one by default
  2. Allow us to access headers so we can edit them prior the request
  3. Maybe provide a couple of methods to manipulate the existing headers (like: resetToken())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions