@@ -165,6 +165,13 @@ class PendingRequest
165165 */
166166 protected $ beforeSendingCallbacks ;
167167
168+ /**
169+ * The callbacks that should execute after the Laravel Response is built.
170+ *
171+ * @var \Illuminate\Support\Collection<int, (callable(\Illuminate\Http\Client\Response): \Illuminate\Http\Client\Response|null)>
172+ */
173+ protected $ afterResponseCallbacks ;
174+
168175 /**
169176 * The stub callables that will handle requests.
170177 *
@@ -268,6 +275,8 @@ public function __construct(?Factory $factory = null, $middleware = [])
268275
269276 $ pendingRequest ->dispatchRequestSendingEvent ();
270277 }]);
278+
279+ $ this ->afterResponseCallbacks = new Collection ();
271280 }
272281
273282 /**
@@ -738,6 +747,19 @@ public function beforeSending($callback)
738747 });
739748 }
740749
750+ /**
751+ * Add a new callback to execute after the response is built.
752+ *
753+ * @param (callable(\Illuminate\Http\Client\Response): \Illuminate\Http\Client\Response|null) $callback
754+ * @return $this
755+ */
756+ public function afterResponse (callable $ callback )
757+ {
758+ $ this ->afterResponseCallbacks [] = $ callback ;
759+
760+ return $ this ;
761+ }
762+
741763 /**
742764 * Throw an exception if a server or client error occurs.
743765 *
@@ -1004,10 +1026,11 @@ public function send(string $method, string $url, array $options = [])
10041026
10051027 return retry ($ this ->tries ?? 1 , function ($ attempt ) use ($ method , $ url , $ options , &$ shouldRetry ) {
10061028 try {
1007- return tap ($ this ->newResponse ($ this ->sendRequest ($ method , $ url , $ options )), function ($ response ) use ($ attempt , &$ shouldRetry ) {
1029+ return tap ($ this ->newResponse ($ this ->sendRequest ($ method , $ url , $ options )), function (& $ response ) use ($ attempt , &$ shouldRetry ) {
10081030 $ this ->populateResponse ($ response );
10091031
10101032 $ this ->dispatchResponseReceivedEvent ($ response );
1033+ $ response = $ this ->runAfterResponseCallbacks ($ response );
10111034
10121035 if ($ response ->successful ()) {
10131036 return ;
@@ -1150,10 +1173,12 @@ protected function makePromise(string $method, string $url, array $options = [],
11501173 {
11511174 return $ this ->promise = $ this ->sendRequest ($ method , $ url , $ options )
11521175 ->then (function (MessageInterface $ message ) {
1153- return tap ($ this ->newResponse ($ message ), function ($ response ) {
1154- $ this ->populateResponse ($ response );
1155- $ this ->dispatchResponseReceivedEvent ($ response );
1156- });
1176+ $ response = $ this ->newResponse ($ message );
1177+
1178+ $ this ->populateResponse ($ response );
1179+ $ this ->dispatchResponseReceivedEvent ($ response );
1180+
1181+ return $ this ->runAfterResponseCallbacks ($ response );
11571182 })
11581183 ->otherwise (function (OutOfBoundsException |TransferException |StrayRequestException $ e ) {
11591184 if ($ e instanceof StrayRequestException) {
@@ -1520,9 +1545,9 @@ protected function sinkStubHandler($sink)
15201545 /**
15211546 * Execute the "before sending" callbacks.
15221547 *
1523- * @param \GuzzleHttp\Psr7 \RequestInterface $request
1548+ * @param \Psr\Http\Message \RequestInterface $request
15241549 * @param array $options
1525- * @return \GuzzleHttp\Psr7 \RequestInterface
1550+ * @return \Psr\Http\Message \RequestInterface
15261551 */
15271552 public function runBeforeSendingCallbacks ($ request , array $ options )
15281553 {
@@ -1579,6 +1604,25 @@ protected function newResponse($response)
15791604 });
15801605 }
15811606
1607+ /**
1608+ * Execute the "after response" callbacks.
1609+ *
1610+ * @param \Illuminate\Http\Client\Response $response
1611+ * @return \Illuminate\Http\Client\Response
1612+ */
1613+ protected function runAfterResponseCallbacks (Response $ response )
1614+ {
1615+ foreach ($ this ->afterResponseCallbacks as $ callback ) {
1616+ $ returnedResponse = $ callback ($ response );
1617+
1618+ if ($ returnedResponse instanceof Response) {
1619+ $ response = $ returnedResponse ;
1620+ }
1621+ }
1622+
1623+ return $ response ;
1624+ }
1625+
15821626 /**
15831627 * Register a stub callable that will intercept requests and be able to return stub responses.
15841628 *
0 commit comments