Skip to content

Commit 59d9f43

Browse files
[9.x] Http client: document retry method $request parameter (#7849)
* Document retry method $request parameter * Small fix in example * Update http-client.md Co-authored-by: Taylor Otwell <[email protected]>
1 parent bb7cd7c commit 59d9f43

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

http-client.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,22 @@ If you would like HTTP client to automatically retry the request if a client or
181181

182182
If needed, you may pass a third argument to the `retry` method. The third argument should be a callable that determines if the retries should actually be attempted. For example, you may wish to only retry the request if the initial request encounters an `ConnectionException`:
183183

184-
$response = Http::retry(3, 100, function ($exception) {
184+
$response = Http::retry(3, 100, function ($exception, $request) {
185185
return $exception instanceof ConnectionException;
186186
})->post(...);
187187

188+
If a request attempt fails, you may wish to make a change to the request before a new attempt is made. You can achieve this by modifying request argument provided to the callable you provided to the `retry` method. For example, you might want to retry the request with a new authorization token if the first attempt returned an authentication error:
189+
190+
$response = Http::withToken($this->getToken())->retry(2, 0, function ($exception, $request) {
191+
if (! $exception instanceof RequestException || $request->response->status() !== 401) {
192+
return false;
193+
}
194+
195+
$request->withToken($this->getNewToken());
196+
197+
return true;
198+
})->post(...);
199+
188200
If all of the requests fail, an instance of `Illuminate\Http\Client\RequestException` will be thrown. If you would like to disable this behavior, you may provide a `throw` argument with a value of `false`. When disabled, the last response received by the client will be returned after all retries have been attempted:
189201

190202
$response = Http::retry(3, 100, throw: false)->post(...);

0 commit comments

Comments
 (0)