调用Azure REST API时如何使用分页?Logic Apps获取VM列表遇分页问题
How to Handle Azure VM List Pagination in Logic Apps
Hey there! I’ve tackled this exact scenario before, so let me walk you through how to get all your VM results using that nextLink you’re seeing, and clear up the HTTP header question upfront.
First: No, You Can’t Use HTTP Headers for This Pagination
Azure’s Compute API (for listing VMs) doesn’t support pagination via HTTP headers like Range or Page. Instead, it uses a nextLink-based pagination pattern—this is the standard approach for many Azure REST APIs.
Step-by-Step to Fetch All VM Results in Logic Apps
Here’s how to build this out in your Logic App:
Initial API Call to Get First 50 VMs
- Add an
HTTPaction to your Logic App. Use theGETmethod and set the URL to:https://management.azure.com/subscriptions/{your-subscription-id}/providers/Microsoft.Compute/virtualMachines?api-version=2023-07-01 - Authenticate with Azure AD (use the "Managed Identity" option if your Logic App has one, or a service principal).
- After running this action, you’ll get a response with:
value: An array of the first 50 VMsnextLink: The URL to fetch the next page of results (if there are more than 50)
- Add an
Parse the Response (Optional but Recommended)
- Add a
Parse JSONaction right after the HTTP call. Use the schema from the VirtualMachineListResult definition (you can generate it automatically by pasting a sample response from your first API call). This makes it way easier to accessvalueandnextLinklater.
- Add a
Set Up a Loop to Fetch Remaining Pages
- First, initialize an array variable (e.g.,
allVMs) to store all your VM results. Append the initialvaluearray to this variable. - Add a
Do Untilloop action. Set the loop condition to:
(Or check if it’s an empty string, depending on how the API returns it when there are no more pages.)@equals(variables('nextLink'), null) - Inside the loop:
- Add another
HTTPaction, and set the URL directly to thenextLinkvariable from your previous response. Use the same authentication as the first call. - Parse this new response with another
Parse JSONaction (reuse the same schema). - Append the new
valuearray to yourallVMsvariable. - Update the
nextLinkvariable to the newnextLinkvalue from this response.
- Add another
- The loop will keep running until
nextLinkis null, meaning you’ve fetched all pages of results.
- First, initialize an array variable (e.g.,
Key Notes to Avoid Headaches
- Permissions: Make sure your Logic App’s identity has the right Azure RBAC permissions (like
Virtual Machine ContributororReader) to list VMs across your subscription. - Rate Limits: Azure has API rate limits—if you have hundreds/thousands of VMs, add a small delay (e.g., 1-2 seconds) inside the loop to avoid getting throttled.
- API Version: Always use the latest stable API version to ensure you’re getting the most reliable pagination behavior.
内容的提问来源于stack exchange,提问作者Beefcake
相关产品推荐
相关产品推荐

