invoke-restmethod parse result and go to next page for data

Hi everyone,

I have a REST API from where I need to get data downloaded, but when I hit api it shows data for one page but there are many pages of data, so overtime I need to edit the query to go to next page.

below is the sample of result, please note the api only accept maximum 300 records in a single page,

page_count : 155
page_number : 1
page_size : 300

to get the next page, I have to use ‘&page_number=2’ in the query, below is the sample query

Invoke-RestMethod ‘ is available for purchase -’ -Method ‘GET’ -Headers $headers -Body $body

I would like to automate the query with a loop and I am bit confused how to do that, I was trying to use the ‘For’ loop but the output is jason when I store it I am not getting the data into a good format, could any one help here?


Let me start by promoting a module I wrote that deals with this:
You’ll want to take a look at the function Get-vROpsResources.

The returned data should include information on how many pages there are, so my get all the information for all pages.
Take a look and let us know how you get on.
(I would have posted some of the code here, but there’s too much and the code just doesn’t reder here for me for some reason).

You can do the following:

$pageCount = 155 # Total number of pages
$urlTemplate = '{0}&#8217' # {0} is substituted later with string format operator
$responses = 1..$pageCount | Foreach-Object {
	$url = $urlTemplate -f $_ # String format operator (-f). {0} becomes $_ value
	Invoke-WebRequest -Uri $url -Method Get -Headers $headers -Body $body
$responses # Contains all responses. $responses[0] is page 1, $responses[1] is page 2, etc.