Logic App and Invoke-RestMethod timeouts

I was working on this Logic App that pulls results from a SharePoint list and outputs via HTTP. The Logic App is triggered via HTTP, in my case using Invoke-RestMethod, and it outputs the results as a response.

This works fine as long as the results are returned in a minute+. If it takes more than 2 minutes Invoke-RestMethod times out. This is even after you add a switch like -TimeoutSec 600 (for 10 mins). Ditto with Invoke-WebRequest.

The solution is to use asynchronous requests with the Logic App. On the Logic App side it’s dead simple. You would usually have a Response block like this:

Click the 3 dots, go to Settings, and toggle it.

On the client/ PowerShell side while I might usually have had code like this:

Now I need to change the behaviour a bit. On the initial HTTP trigger the call will succeed immediately; but the headers of the result will contain a location field header which has a URL I can keep polling while the status code is 202. This URL will have the results from the Logic App assuming it exits cleanly with a status code 200.

I can’t use Invoke-RestMethod any more as that has no way of looking at the headers. So Invoke-WebRequest it is. The code now becomes this:

Neat!