Invoke-WebRequest with retry

As a continuation to a previous post where I was using Invoke-RestMethod to send some output to a Logic App’s callback URL I stumbled into an edge case wherein occassionally the callback would fail and rather than retry (because I was an idiot who forgot to put a try/ catch around it) the Function would fail.

Here’s an example error I get when calling the callback URL (this is not the entire message):

I could add a try/ catch, but more importantly I want to keep retrying until it succeeds. Turns out Invoke-RestMethod doesn’t give status code output so I had to resort to Invoke-WebRequest instead and do the following:

Now the Function keeps trying until it gets a 200 OK.

I should probably add some code/ counter to stop trying after a while. Doesn’t matter in this case as the Function has a timeout of 30 mins, while the webhook action is set to timeout after 60 mins, so the Function will timeout before that.