title: Powershell pagination with page token
body: <p>I am trying to curl an API on loop while the page token is not empty.
  Essentially get all the results. I am having difficulties implementing the pagination.</p>
  <pre><code>$headers = New-Object &quot;System.Collections.Generic.Dictionary[[String],[String]]&quot;
  $headers.Add(&quot;Accept&quot;, &quot;application/json&quot;)
  $headers.Add(&quot;Authorization&quot;, &quot;Basic *************************&quot;)
  $headers.Add(&quot;Content-Type&quot;, &quot;application/json&quot;)
  
  $body = &quot;{
  `n    `&quot;aggregations`&quot;: [
  `n        {
  `n            `&quot;metric`&quot;: `&quot;io.confluent.kafka.server/received_bytes`&quot;
  `n        }
  `n    ],
  `n    `&quot;filter`&quot;: {
  `n        `&quot;field`&quot;: `&quot;resource.kafka.id`&quot;,
  `n        `&quot;op`&quot;: `&quot;EQ`&quot;,
  `n        `&quot;value`&quot;: `&quot;********`&quot;
  `n    },
  `n    `&quot;granularity`&quot;: `&quot;P1D`&quot;,
  `n    `&quot;group_by`&quot;: [
  `n        `&quot;metric.topic`&quot;
  `n    ],
  `n    `&quot;intervals`&quot;: [
  `n        `&quot;now-7d/now`&quot;
  `n    ]
  `n}&quot;
  
  $response = Invoke-RestMethod 'https://api.telemetry.confluent.cloud/v2/metrics/cloud/query' -Method 'POST' -Headers $headers -Body $body 
  $Nextpage = $response.meta.pagination.next_page_token
  $url = 'https://api.telemetry.confluent.cloud/v2/metrics/cloud/query?next_page_token='
  
  while ($Nextpage -ne $null){
      $response = Invoke-RestMethod $url$Nextpage -Method 'POST' -Headers $headers -Body $body | ConvertTo-Json
      $Nextpage = $response.meta.pagination.next_page_token
  }
  </code></pre>