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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

404 error when loading data from REST API which paginates to model


Hi All,

 

I am pulling data from REST API that is paginated with the below M Query,

 

let
Pagination = List.Skip(List.Generate( () => [WebCall=[], Page = 1, Counter=0], // Start Value
   		each List.Count(Record.FieldNames([WebCall]))>0 or [Counter]=0, // Condition under which the next execution will happen
   		each [ WebCall = Json.Document(Web.Contents("http://py-soconnect.fusesport.com/api/members/for-season/250/?page="&Text.From([Page])&"")), // retrieve results per call
     			Page = [Page]+1,
     			Counter = [Counter]+1// internal counter
]
) ,1),
    #"Converted to Table" = Table.FromList(Pagination, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
    #"Converted to Table"

However the last record is a ERROR record and the error message is as follows,

 

DataSource.Error: Web.Contents failed to get contents from 'http://py-soconnect.fusesport.com/api/members/for-season/250/?page=246' (404): Not Found
Details:
    DataSourceKind=Web
    DataSourcePath=http://py-soconnect.fusesport.com/api/members/for-season/250
    Url=http://py-soconnect.fusesport.com/api/members/for-season/250/?page=246

 I am unable to load the data into the model. Any help with this be highly appreciated.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Hi @Anonymous,

 

I modify my condition and double check to confirm it can get the last page records:

let
Pagination = List.Skip(List.Buffer(List.Generate( () => [WebCall=[], Page = 0, Counter=0], // Start Value
   		each try Record.Field([WebCall],"next")<>null or (Record.Field([WebCall],"next")=null and [Counter]>0) otherwise false or [Counter]=0, // Condition under which the next execution will happen
   		each [ WebCall = Json.Document(Web.Contents("http://py-soconnect.fusesport.com/api/members/for-season/250/?page="&Text.From([Page])&"")), // retrieve results per call
     			Page = [Page]+1,
     			Counter = [Counter]+1// internal counter
]
)),1),
    #"Converted to Table" = Table.FromList(Pagination, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Column1 = #"Converted to Table"{19}[Column1]
in
    Column1

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

4 REPLIES 4
v-shex-msft
Community Support
Community Support

Hi @Anonymous,

 

After I test with url you shared, I found your api will return both previous and next request url at same time. I think you need to add condition to check response contents to confirm if it contains next page result.

let
Pagination = List.Skip(List.Buffer(List.Generate( () => [WebCall=[], Page = 1, Counter=0], // Start Value
   		each try Record.Field([WebCall],"next")<>null otherwise false or [Counter]=0, // Condition under which the next execution will happen
   		each [ WebCall = Json.Document(Web.Contents("http://py-soconnect.fusesport.com/api/members/for-season/250/?page="&Text.From([Page])&"")), // retrieve results per call
     			Page = [Page]+1,
     			Counter = [Counter]+1// internal counter
]
)),1),
    #"Converted to Table" = Table.FromList(Pagination, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
    #"Converted to Table"

 

BTW, Buffer function is used to solve the memory issue for looping functions when it save change and generate data model.

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Hi @v-shex-msft

 

I see that there is a data loss with the above code as the records in the last page are not pulled. 

 

 

  "count": 24628,
  "next": "http://py-soconnect.fusesport.com/api/members/for-season/250/?page=2",
  "previous": null,
  "results": [

 The total number of records are 24628 and thus 246 pages , but this query returns only 245 records. Could you suggest a way to get records from the last page as well.

 

Thanks

Hi @Anonymous,

 

I modify my condition and double check to confirm it can get the last page records:

let
Pagination = List.Skip(List.Buffer(List.Generate( () => [WebCall=[], Page = 0, Counter=0], // Start Value
   		each try Record.Field([WebCall],"next")<>null or (Record.Field([WebCall],"next")=null and [Counter]>0) otherwise false or [Counter]=0, // Condition under which the next execution will happen
   		each [ WebCall = Json.Document(Web.Contents("http://py-soconnect.fusesport.com/api/members/for-season/250/?page="&Text.From([Page])&"")), // retrieve results per call
     			Page = [Page]+1,
     			Counter = [Counter]+1// internal counter
]
)),1),
    #"Converted to Table" = Table.FromList(Pagination, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Column1 = #"Converted to Table"{19}[Column1]
in
    Column1

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @v-shex-msft 

 

Hope you can shed some light. The API restricts to bring the data upto 100 rows per call. So, I have found the below query to run however the condition goes on an infinite loop where the query retrieves null data even there is no data found. For example as shown in the image below.  I have found that there is a record.field called "hasMore" - True or False (attached images - Row 7 contains hasmore as True and Row 8 contains has more as False) which determines whether to see if there are more data on each page. I am just trying different different queries to see how this can be adjusted for exam: when the query hits Hasmore = False  then query should stop retrieving data as I think this probably the solution

let
Pagination = List.Skip(List.Generate( () => [WebCall=[], Page = 0, Counter=0], // Start Value
   		each List.Count(Record.FieldNames([WebCall]))>0 or [Counter]=0, // Condition under which the next execution will happen
   		each [ WebCall = Json.Document(Web.Contents("https://energycloud.com/api/Supplier?apiKey=a1459b&orderby=name&page="&Text.From([Page])&"")), // retrieve results per call
     			Page = [Page]+1,
     			Counter = [Counter]+1// internal counter
]
) ,1),
    #"Converted to Table" = Table.FromList(Pagination, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"WebCall", "Page", "Counter"}, {"WebCall", "Page", "Counter"}),
    #"Expanded WebCall" = Table.ExpandRecordColumn(#"Expanded Column1", "WebCall", {"items", "hasMore"}, {"items", "hasMore"})
in
    #"Expanded WebCall"

.  

 

 

Capture.JPG  

 

 

 

 

hasmore_false.JPGhasmore_True.JPG 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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