loop over parameterized rest api call

Viewed 49

I'm trying to do a history load of data from a ReST api call using powershell. If I were to call the ReST endpoint with no parameters I either run into timeout or out of memory exceptions.

$Header = @{
    "authorization" = "Bearer $token"
}
#make REST API call
$Parameters = @{
    Method      = "GET"
    Headers     = $Header
    ContentType = "application/json"
    Body        = $BodyJson
}
$startYear = 2014
$endYear = 2022

$Data = {for($year=$startYear; $i -le $endYear; $year=$year+1) {Invoke-RestMethod -Uri "https://api.mysite.com/v1/data/year/Year/" + [string]$year @params -DisableKeepAlive -ErrorAction Stop}} | ConvertTo-Json

tried the below as well.

    $Data = foreach($year in $startYear..$endYear) {
        $url = "https://api.mysite.com/v1/data/year/Year/" + [string]$year 
        Invoke-RestMethod -Uri $url @params -DisableKeepAlive -ErrorAction Stop}

$Data | ConvertTo-Json

I don't see all of the data starting from the year 2010 until the year 2022. Also, I would like to append $Data with all the data starting from the year 2010 until the current year.

0 Answers
Related