Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

Loop through API response with a next page url

I'm working with an endpoint where I am sending a POST request and some parameters and get a JSON response with the data I have requested. However, this only returns the first page of the results along with the "next_page_url" I want to loop trough all the pages and return the data as a single table.

 

This is what I have come up till now.
However, this returns only the first page with some Error.

 

w_soham_more_0-1721300719824.png

 

Click on Error:

w_soham_more_1-1721300757513.png

Click on Result:

w_soham_more_2-1721300791285.png

The "results" is the response from the first page of the request. 

The "next_page_url" gives the URL for the second page of the results.

 

The Query is as follows:

 

 

 

= let
    Source = List.Generate( () =>
    
        [ 
            URL = "https://website.com/endpoint?script=1111" , 
            headers = [#"Content-Type" = "application/json", #"Method" = "POST", #"User-Agent" = "Mozilla/5.0"],
            postData = Json.FromValue([ #"q1" = "a1"]),
            Result = Json.Document(Web.Contents(
                URL, 
                [
                    Headers = headers,
                    Content = postData
                ]
            ))            
        ],
    
        each [Result][next_page_url] <> null,
    
        each [URL = [Result][next_page_url],
        headers = [#"Content-Type" = "application/json", #"Method" = "POST", #"User-Agent" = "Mozilla/5.0"],
        postData = Json.FromValue([ #"q1" = "a1"]),
        Result = Json.Document(Web.Contents(
            URL, 
            [
                Headers = headers,
                Content = postData
            ]
        )) 
        ]
    
    )
in
    Source

 

 

 

What am I doing wrong here?

 

I have gone through many similar questions but could not find anything that worked for me. 

 

Thanks!

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

Your current query can only fetch the URL once and not recursively. I think you could write a function that requests your API, then takes the next page url at each result you traverse, then puts that url into the custom function above to access it and then proceeds to take the next page url.
For example:

fx(next page url) = > 
URL = next page url , 
            headers = [#"Content-Type" = "application/json", #"Method" = "POST", #"User-Agent" = "Mozilla/5.0"],
            postData = Json.FromValue([ #"q1" = "a1"]),
            Result = Json.Document(Web.Contents(
                URL, 
                [
                    Headers = headers,
                    Content = postData
                ]
            ))

Then refer to this function in your query.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Anonymous ,

Your current query can only fetch the URL once and not recursively. I think you could write a function that requests your API, then takes the next page url at each result you traverse, then puts that url into the custom function above to access it and then proceeds to take the next page url.
For example:

fx(next page url) = > 
URL = next page url , 
            headers = [#"Content-Type" = "application/json", #"Method" = "POST", #"User-Agent" = "Mozilla/5.0"],
            postData = Json.FromValue([ #"q1" = "a1"]),
            Result = Json.Document(Web.Contents(
                URL, 
                [
                    Headers = headers,
                    Content = postData
                ]
            ))

Then refer to this function in your query.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @Anonymous ,

 

Thank you for the response. That was helpful. 
I have created a function that pulls the records and then referencing that function in List.Generate.

The List.Generate throws the following error:

w_soham_more_0-1722940834801.png

 

 

This is the query:

= let
Source = List.Generate(
[Result = Query1("url"),
],
each [Result][next_page_url] <> null,
each [Query1([Result][next_page_url])],
each [Result][results]
)
in
Source


How do I resolve this issue?

Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.