@@ -479,6 +479,59 @@ instead. You can force select a transport adapter using a constructor option::
479479
480480 $client = new Client(['adapter' => Stream::class]);
481481
482+ Events
483+ ======
484+
485+ The HTTP client triggers couple of events before and after sending a request
486+ which allows you to modify either the request or response or do other tasks like
487+ caching, logging etc.
488+
489+ HttpClient.beforeSend
490+ ---------------------
491+
492+ // Somewhere before calling one of the HTTP client's methods which makes a request
493+ $http->getEventManager()->on(
494+ 'HttpClient.beforeSend',
495+ function (
496+ \C ake\H ttp\C lient\C lientEvent $event,
497+ \C ake\H ttp\C lient\R equest $request,
498+ array $adapterOptions,
499+ int $redirects
500+ ) {
501+ // Modify the request
502+ $event->setRequest(....);
503+ // Modify the adapter options
504+ $event->setAdapterOptions(....);
505+
506+ // Skip making the actual request by returning a response.
507+ // You can use $event->setResult($response) to achieve the same.
508+ return new \C ake\H ttp\C lient\R esponse(body: 'something');
509+ }
510+ );
511+
512+ HttpClient.afterSend
513+ ---------------------
514+
515+ // Somewhere before calling one of the HTTP client's methods which makes a request
516+ $http->getEventManager()->on(
517+ 'HttpClient.afterSend',
518+ function (
519+ \C ake\H ttp\C lient\C lientEvent $event,
520+ \C ake\H ttp\C lient\R equest $request,
521+ array $adapterOptions,
522+ int $redirects,
523+ bool $requestSent // Indicates whether the request was actually sent
524+ // or response returned from ``beforeSend `` event
525+ ) {
526+ // Get the response
527+ $response = $event->getResponse();
528+
529+ // Return a new/modified response.
530+ // You can use $event->setResult($response) to achieve the same.
531+ return new \C ake\H ttp\C lient\R esponse(body: 'something');
532+ }
533+ );
534+
482535.. _httpclient-testing :
483536
484537Testing
0 commit comments