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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
w_soham_more
Frequent Visitor

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 @w_soham_more ,

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 @w_soham_more ,

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.

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
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors