caseyamcl / guzzle_retry_middleware

Middleware for Guzzle v6/7+ that automatically retries HTTP requests on 429, 503 responses.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set new header option in retry callback

whobutsb opened this issue · comments

Is there a way to set a new header option with each retry callback? For example if I receive a 429 rate limiting error and wanted to use a different JWT access token. Can I modify the request object to send a different request?

Example:

        $listener = function($attemptNumber, $delay, &$request, &$options, $response) {
            $request->setHeader('x-token', $this->access_tokens[$attemptNumber]);
        };

Thank you for the assistance.

There certainly is! However, the Request object is immutable, so you have to overwrite the reference inside of your callback function. The following syntax should do what you need:

        $listener = function($attemptNumber, $delay, &$request, &$options, $response) {
            $request = $request->withHeader('x-token', $this->access_tokens[$attemptNumber]);
        };

To see example code of this in action, refer to the testRetryCallbackReferenceModification in the test file.

Thank you for the helpful reply! It is much appreciated!