🔧
HTTP Client Config
Like headers and query parameters, HTTP config can be added by using the
config()
method on either the connector or the request. When you add config to a connector instance, every request sent through that connector will merge those configuration options with the request. When you add config to a request instance, it will just be added to that one request instance.Saloon's default sender is the GuzzleSender. By default, the config options are added to the guzzle request. You can use any of the options Guzzle provide in the Saloon's config. Click here to see a list of the available options Guzzle provide.
You may configure the default HTTP config on the connector or request using the protected
defaultConfig
method.Connector
Request
<?php
use Saloon\Http\Connector;
class ForgeConnector extends Connector
{
// {...}
protected function defaultConfig(): array
{
return [
'timeout' => 30,
];
}
}
Default HTTP config on a connector will be applied to every request that uses the connector.
<?php
use Saloon\Http\Request;
class GetServersRequest extends Request
{
// {...}
protected function defaultConfig(): array
{
return [
'timeout' => 30,
];
}
}
You may also use properties in requests to populate HTTP config, for example - specifying an exact timeout on a per-request basis.
Definition
Usage
<?php
use Saloon\Http\Request;
class GetServersRequest extends Request
{
// {...}
protected int $timeout;
public function __constructor(int $timeout)
{
$this->timeout = $timeout;
}
protected function defaultConfig(): array
{
return [
'timeout' => $this->timeout,
];
}
}
<?php
$request = new GetServersRequest(timeout: 30);
Saloon also offers a handy config API to manage them easily after a request instance has been created. Use the
config()
method on your request to manage them. HTTP config added to the request is prioritised more than the connector's config. This is useful for changing them before a request is sent.<?php
$request = new GetServersRequest();
$request->config()->add('timeout', 30);
$all = $request->config()->all();
// array: ['timeout' => 30]
When you have default HTTP config on a connector, they won't be visible to the request instance as they are merged later in the request lifecycle. Still, request HTTP config will have a higher priority than connector config.
Overwrite the config on the request with a new array.
Merge arrays of config.
Remove a given config option by its key.
Get a givenconfig option by its key or return the default.
Retrieve all config options as an array.
Check if the config object is empty.
Click here to view the API reference for this method.
Last modified 28d ago