0

I have an API http://xx.yy.zz.aaa:8080/api/2.0/GetVersion which I need to hit from Powershell script. When I try hitting it using Invoke-RestMethod it gives me response in 10 minutes.

enter image description here

When I hit same API using Invoke-WebRequest I get response in same 10 minutes.

enter image description here

I added $ProgressPreference = 'SilentlyContinue' before making API call but seems like it has no effect.

Whereas if I hit the same API from PowerShell using Invoke-WebRequest or Invoke-RestMethod I get response within seconds. My powerShell version is 5.1.14393.4530

enter image description here

Here is my script:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$pair = "$($username):$($password)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$headers.Add("Authorization", "Basic $encodedCreds")


function verifyDeployment($vapor_env, $headers) {
    try {
        $uri = "http://{0}:8080/api/2.0/{1}" -f "xx.yy.zz.aaa", "GetVersion"
        $ProgressPreference = 'SilentlyContinue'
        $response = Invoke-WebRequest -URI $uri -Method Get -Headers $headers -UseBasicParsing
        $statusCode = $response | Select-Object -Expand StatusCode
        Write-Output $response, $uri, $statusCode, $api
        if ($statusCode -eq 200) {
            $version = $response | Select-Object -Expand Content | ConvertFrom-Json | Select-Object -Expand Version
            if ($version -eq "12.14.90") {
                Write-Output "Version matched."
            }
            else {
                Write-Output "Version didn't match."
            }
        }
        else {
            Write-Output "Couldn't hit API."
        }
    }
    catch {}
}


verifyDeployment $vapor_env $headers

What can I do to reduce response time.

Thanks.

CK__
  • 1,252
  • 1
  • 11
  • 25
  • So when you run invoke-webrequest or invoke-restmethod directly it is instant but when you run the same command in a script it takes 10 minutes? Can you share the script. We would need to see in order to help. – Daniel Aug 27 '21 at 06:40
  • @Daniel, you are right, invoke-webrequest or invoke-restmethod directly it is instant but when you run the same command in a script it takes 10 minutes. Also I have added the script now. – CK__ Aug 27 '21 at 07:01
  • What is parameter `$vapor_env` for, since the function does not seem to use it? Personally I would do `[version]$version = ($response.Content | ConvertFrom-Json).Version` and compare with `if ($version -eq [version]'12.14.90') {...}` – Theo Aug 27 '21 at 12:28
  • 1
    To me it makes no sense why the function would take so long. Did you leave out some code? I would step through the code with a debugger to see where you are getting the delay. I don't believe the delay is coming from invoke-webrequest – Daniel Aug 27 '21 at 18:28
  • Try to set `-TimeoutSec` explicitly and capture network trace when debugging to check the timing when the actual request happens, maybe your server has default timeout settings modified – andr3yk Aug 27 '21 at 22:06

1 Answers1

1

The issue was due to heavy response data from API. Now I am using pagination and it has reduced the response time drastically.

CK__
  • 1,252
  • 1
  • 11
  • 25