In a custom module, I have a custom service that uses GuzzleHttp\Client service.
mymodule.services.yml:
services: mymodule.mymodule_call: class: Drupal\mymodule\MyModuleCall arguments: ['@http_client'] The MyModuleCall is using dependency injection and start like:
namespace Drupal\mymodule; use GuzzleHttp\Client; class MyModuleCall { protected $client; public function __construct(Client $client) { $this->client = $client; } } As explained in the GuzzleHttp documentation (http://docs.guzzlephp.org/en/latest/quickstart.html), we can pass an argument to the Client constructor to set the base_uri.
Like so:
use GuzzleHttp\Client; $client = new Client([ // Base URI is used with relative requests 'base_uri' => 'http://httpbin.org' ]); How do we set the 'base_uri' in the constructor when we use dependency injection to instantiate a class?