Home > Enterprise >  Powershell, calling API with Invoke-RestMethod results in double execution
Powershell, calling API with Invoke-RestMethod results in double execution

Time:04-01

I have an API, which I'm calling in Powershell using the Invoke-Restmethod like this

Invoke-RestMethod -Method GET -Uri http://localhost:3030/api/startSync -TimeoutSec 2147483647

This results in the API method being called twice. One time immediately, and another time exactly 120 seconds after the first call. If I call the API method using another client, it gets called only once (so it seems not to be a problem within the API method itself, but in the Invoke-RestMethod call). I could not find any documentation about this. What is the reason for this and how can I prevent it?

CodePudding user response:

Apparently there is some undocumented behaviour when using the PowerShell commands Invoke-Restmethod and Invoke-WebRequest: when the TimeoutSec option is used, the command is repeated 120 seconds after the first call, if there is no response until that. If the TimeoutSec option is not used, this does not happen (but there is an error message after ca. 115 seconds).

Tested with PowerShell 5.

Solution: run the command without TimeoutSec or modify the API call so that a result is returned before 120 seconds, even though the job continues running in the background.

  • Related