Search code examples
powershellinvoke-webrequestinvoke-restmethod

Invoke-RestMethod is taking longer time to get response from Rest API


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.


Solution

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