Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rizdhanhr's avatar

laravel http retry based on condition.

excuse me, im a junior dev, still struggle learn a lot about laravel. i want to ask how can i retry the http post based on condition? example i want to retry if the response->id != 1; $response = Http::retry(3, 100, function () { //if else condition })->get('https://fakestoreapi.com/products/1');

thanks.

0 likes
9 replies
Snapey's avatar

You think you will get a different response if you ask more than once?

Assuming there are no http errors then the data is the data, It would be an extremely poor API implementation if you got a better result if you ask again immediately.

duddyrosenberg's avatar

@Snapey I actually have a good use case for this. I’m requesting a server to generate a report and i would like to check every half minute or so if the report is ready. I'm assuming I’m gonna have to write this myself but it be cool if i was somehow able to use retry()

tisuchi's avatar
tisuchi
Best Answer
Level 70

@rizdhanhr This is how it works.

use Illuminate\Support\Facades\Http;

$response = Http::retry(3, 100, function ($response, $attempts) {
    // This condition will be checked after each failed attempt to see if we should retry again.
    // If the condition returns 'true', then the request will be retried.
    return $response->json()['id'] != 1;
})->get('https://fakestoreapi.com/products/1');

// Handle the $response as needed.
3 likes
rodrigo.pedra's avatar

From the docs:

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.

https://laravel.com/docs/10.x/http-client#retries

Try this:

// According to the docs, the third argument is a closure which
// is responsible to tell the HTTP Cliente if the request should
// be retried
$response = Http::retry(3, 100, function ($exception) {
    // $exception is a \Illuminate\Http\Client\RequestException
    // which holds the response as a public property
    //
    // the response is a \Illuminate\Http\Client\Response instance
    // which implements \ArrayAccess, but does not have
    // a __get() magic method.
    //
    // Therefore, let's use array notation to get the 'id'
    // attribute from it
    $id = $exception->response['id'] ?? null;
    
    return $id !== 1;
})->get('https://fakestoreapi.com/products/1');
1 like
rodrigo.pedra's avatar

@rizdhanhr one tip for your following inquiries

Use single backticks, to wrap inline code, for example, if you had written your first paragraph like this:

excuse me, im a junior dev, still struggle learn a lot about laravel. i want to ask how can i retry the http post based on condition? example i want to retry if the `response->id != 1;`

The output would be this (notice the backticks around response->id != 1;)

excuse me, im a junior dev, still struggle learn a lot about laravel. i want to ask how can i retry the http post based on condition? example i want to retry if the response->id != 1;

For blocks of code, you should wrap them around two lines, both containing a sequence of three backticks. For example:

```
$response = Http::retry(3, 100, function () { 
    //if else condition
})->get('https://fakestoreapi.com/products/1');
```

Gives this output:

$response = Http::retry(3, 100, function () { 
    //if else condition
})->get('https://fakestoreapi.com/products/1');

This last part is optional.

Although the syntax highlighter used by Laracasts does an impressive job on identifying the language used in a block of code, you can help the highlighter by "hinting" which language you are using.

With language hinting, the code block above would look like this:

```php
$response = Http::retry(3, 100, function () { 
    //if else condition
})->get('https://fakestoreapi.com/products/1');
```

Use these tips, and I am sure you will get faster responses.

Have a good day, and welcome to the journey of being a developer.

I wish you the best luck =)

triadi's avatar

How to retry HTTP get if only the request was timeout?

this is my code

 private function createRequest(): PendingRequest
    {
        return Http::asForm()
            ->retry(self::RETRY_TIMES, self::RETRY_DELAY_MILISECONDS)
            ->timeout(self::TIMEOUT_SECONDS)
            ->baseUrl(config('services.e-office.proxy'))
            ->withoutVerifying()
            ->withHeaders([
                'Authorization' => $this->token(),
            ])
           ;
    }

Please or to participate in this conversation.