Skip to content

Conversation

@xurshudyan
Copy link
Contributor

This pull request adds a new feature to the Laravel HTTP client that allows global default options to be set for all requests. This makes it possible to define headers, timeouts, and other options that will be used by default for all HTTP requests.

Example Usage:
You can set global default options for all HTTP requests by calling the withDefaultOptions method within the boot method of your App\Providers\AppServiceProvider class. Here's an example:

Http::withDefaultOptions([
    'headers' => [
        'Authorization' => 'Bearer my-secret-token',
    ],
    'timeout' => 10,
]);

This sets the Authorization header and a 10 second timeout for all requests made using the Laravel HTTP client.

To remove one or more default options, use the withoutDefaultOptions method. For example:

Http::withoutDefaultOptions('headers.Authorization')->get('https://foo.com');

This removes the Authorization header from the default options.

If you need to remove multiple default options, you can pass an array of keys to the withoutDefaultOptions method:

Http::withoutDefaultOptions(['headers.Authorization', 'timeout'])->get('https://foo.com');

This removes the Authorization header and the timeout option from the default options.

Another example.

Http::withDefaultOptions([
    'base_uri' => 'https://api.example.com/v1/',
]);

$response = Http::get('users');

// This sends a GET request to https://api.example.com/v1/users

@abenerd
Copy link
Contributor

abenerd commented Mar 30, 2023

I like the idea but I wonder what the impact would be for vendor code using the http client. I can imagine this breaking things or leaking stuff.

@crynobone
Copy link
Member

On one hand yes this would have the potential to break vendor code but in certain environments where any external HTTP request needs to use a proxy there is no way at the moment for any developer to add proxy options for vendor code.

Previous attempt: #40332

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants