I am calling an API 500 times with 10 parallel threads as part of load testing. I want to capture the result of API call in a global variable (a counter outside script block scope) so, that I can process further for validation.
Example- In below code , I want to check if all 500 API call is success or not.
PFB code snippet-
$invokeAPI =
{
try {
$bodyContent = Get-Content $Using:inputFilepath
$Response = (Invoke-WebRequest -Method 'Post' -Uri $Using:headUri -Headers $Using:blobHeaders -Body $bodyContent).StatusCode
Write-Host -BackgroundColor Green "status Code :" $Response
}
catch [System.Exception] {
Write-Host -ForegroundColor Red "Exception caught while invoking API :" $_.ErrorDetails.Message
[int]$_.Exception.Response.StatusCode
}
}
1..500 | ForEach-Object -Parallel $invokeAPI -ThrottleLimit 10
<# ToDo...Capture API invocation Result to validate results#>