Skip to content

Add request headers on demand

Compare
Choose a tag to compare
@gocanto gocanto released this 14 Nov 03:53
· 39 commits to master since this release
8e9a4f5

Sometimes, we need to add headers to a given client instance based on dynamic data.

Such as requirement is not possible on the creation stage because we do not know what information we would be dealing with. Therefore, we need a mechanism to hook into and populate this data when needed.

This PR adds a new method withHeaders to allow on-demand headers values whenever needed.

For instance, you would be able to do something like so:

$client = new HttpClient;

$client->withHeaders([
      'X-GUS-1' => 'testing testing',
      'X-GUS-2' => 'testing testing',
      'X-GUS-3' => 'testing testing',
      'X-GUS-4' => 'testing testing',
])->request('GET', 'https://foo.com');

to populate as many headers as needed.

Note

You can still call other methods on the client because of the immutability nature of the given method.


You will be able to see the PR body here