There's been a few similar posts on this but none quite the same (or they don't describe the issue clearly, if they are the same).
I've got a script which makes one API call using Invoke-RestMethod and then iterates over the result of that call making other calls with values from it. The value substitution etc is fine, and I can see (both by dumping the Uri out with Write-Host, and also from the API logs itself) that the calls are being made successfully.
If I do this:
$team = @{ "id" = 5 }
$response = Invoke-RestMethod -Uri https://theUri/$($team.id) -Headers $header -Method Get
then $response is populated as you'd expect and contains the json response from the API endpoint. Response time is a few hundred milliseconds.
If, on the other hand, I have an array of team objects and do this:
foreach ($team in $teams)
{
$response = Invoke-RestMethod -Uri https://theUri/$($team.id) -Headers $header -Method Get
Write-Host $response
}
then it iterates over the whole of the $teams array and outputs empty values on each of the Write-Host lines (the same occurs no matter what you do with it, I'm just using Write-Host as an example here; sending it to Select-Object etc shows it's still blank).
Is this deliberate? I assume no, because it's mad. I've got a temporary workaround by (yes, it's this awful...) writing out the results of the first Api call to a file, then iterating over that list calling a script that does the subsequent calls without the foreach loop in the script for each row. Is there a better way?
I've tried -DisableKeepAlive - no difference. I've tried setting the -TimeoutSec value to a small number, a big number, etc - no difference. I've tried -OutFile and that writes out a bunch of empty files.
It feels - without any evidence for this - that when called inside the loop it's waiting for the HTTP response (which is a 200) and then not downloading any of the response body.
This is Powershell 7.1.3, btw.
Cheers in advance!