Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
I'm encountering an issue while integrating LeafLink's API into Power BI using Web.Contents. Everything works fine in Power BI Desktop, but after publishing to the Power BI Service, the scheduled refresh fails with the following error:
DataSource.Error: Web.Contents failed to get contents from 'https://app.leaflink.com/api/v2/products/' (400): Bad Request
In my M code, I define the base URL with a trailing slash, but it seems that when the query runs in the Power BI Service, the trailing slash is trimmed. This results in a malformed URL that the LeafLink API rejects. Question: Is this a known behavior/limitation with Web.Contents in Power BI Service where trailing slashes in base URLs are trimmed? If so, would Microsoft consider this expected behavior or something worth documenting?
Looking forward to any insights from the community or MS team!
Best regards,
Data IKseer
here is my M Code :
let
// Define the limit per page; adjust as needed.
limit = 100,
// Base URL for the API endpoint.
baseUrl = "https://app.leaflink.com/api/v2/products/",
// Your API token.
token = "XXXXXXXXXX",
// Function to fetch a page of data based on offset.
fetchPage = (offset as number) =>
let
query = [
limit = Number.ToText(limit),
offset = Number.ToText(offset)
],
Source = Web.Contents(
baseUrl,
Headers = [
#"Authorization" = "Token " & token,
#"Content-Type" = "application/json"
],
Query = query
),
JsonResponse = Json.Document(Source)
in
JsonResponse,
// Fetch the first page to get the total record count.
firstPage = fetchPage(0),
totalCount = firstPage[count],
// Generate a list of pages using the offset.
pages = List.Generate(
// Initial state: start with offset=0 and the first page.
() => [offset = 0, page = firstPage],
// Continue while the current offset is less than totalCount.
each [offset] < totalCount,
// Increment offset by the limit and fetch the next page.
each [offset = [offset] + limit, page = fetchPage([offset] + limit)]
),
// Extract pages from the state records.
// The first page is already retrieved; add the rest.
allPages = {firstPage} & List.Transform(pages, each [page]),
// Combine the results lists from each page.
// (If a page has no "results" field, it safely returns an empty list.)
resultsList = List.Combine(List.Transform(allPages, each try _[results] otherwise {}))
in
#"resultsList "
Solved! Go to Solution.
Hi @DataIkseer
Power BI Service treats Web.Contents
differently — particularly in how it constructs and evaluates the base URL and relative paths. One specific behavior is that:
Trailing slashes in the base URL can be trimmed or misinterpreted when used directly in Web.Contents
, especially if you're using query parameters or dynamic URL construction.
This is more likely to cause issues if:
The API requires an exact match of the endpoint structure, including trailing slashes.
You're using the Web.Contents
overload that splits base URL and query, and the service tries to "optimize" or pre-evaluate URLs for gateway compatibility.
Use the fully constructed URL (with query parameters) as a single string to avoid Power BI Service manipulating the structure.
Here's how to rework your fetchPage
function safely:
fetchPage = (offset as number) =>
let
fullUrl = baseUrl & "?" & Uri.BuildQueryString([
limit = Number.ToText(limit),
offset = Number.ToText(offset)
]),
Source = Web.Contents(
fullUrl,
[
Headers = [
#"Authorization" = "Token " & token,
#"Content-Type" = "application/json"
]
]
),
JsonResponse = Json.Document(Source)
in
JsonResponse
This quirk isn't very well-documented, but it's been raised in several forums and GitHub issues, especially around:
Relative path handling
Base URL and privacy levels
URL encoding and trailing slash behavior
So yes — it's expected behavior but under-documented. You’re not doing anything wrong; this is a subtle platform inconsistency between Desktop and Service.
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
Hi @DataIkseer ,
Thank you for reaching out to Microsoft Fabric Community Forum.
@johnbasha33 Thank you for your quick response.
Alongside the solution provided by the super user, you may also consider trying the following.
Yes, this is a known behavior — the way Web.Contents handles URLs can differ between Power BI Desktop and the Service. To avoid this issue, it’s recommended to use the RelativePath option inside Web.Contents. This ensures Power BI constructs the full URL properly without trimming or misinterpreting it.
let
limit = 100,
baseUrl = "https://app.leaflink.com/api/v2/",
token = "XXXXXXXXXX",
fetchPage = (offset as number) =>
let
query = [
limit = Number.ToText(limit),
offset = Number.ToText(offset)
],
Source = Web.Contents(
baseUrl,
[
RelativePath = "products/",
Query = query,
Headers = [
#"Authorization" = "Token " & token,
#"Content-Type" = "application/json"
]
]
),
JsonResponse = Json.Document(Source)
in
JsonResponse,
firstPage = fetchPage(0),
totalCount = firstPage[count],
pages = List.Generate(
() => [offset = 0, page = firstPage],
each [offset] < totalCount,
each [offset = [offset] + limit, page = fetchPage([offset] + limit)]
),
allPages = {firstPage} & List.Transform(pages, each [page]),
resultsList = List.Combine(List.Transform(allPages, each try _[results] otherwise {}))
in
resultsList
If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! |
Regards,
B Manikanteswara Reddy
Hi @DataIkseer ,
We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.
Please don't forget to give a "Kudos |
Regards,
B Manikanteswara Reddy
Hi @DataIkseer ,
As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.
Please don't forget to give a "Kudos |
Regards,
B Manikanteswara Reddy
Hi @DataIkseer ,
May I ask if you have gotten this issue resolved?
If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.
Please don't forget to give a "Kudos |
Regards,
B Manikanteswara Reddy
Hi @DataIkseer
Power BI Service treats Web.Contents
differently — particularly in how it constructs and evaluates the base URL and relative paths. One specific behavior is that:
Trailing slashes in the base URL can be trimmed or misinterpreted when used directly in Web.Contents
, especially if you're using query parameters or dynamic URL construction.
This is more likely to cause issues if:
The API requires an exact match of the endpoint structure, including trailing slashes.
You're using the Web.Contents
overload that splits base URL and query, and the service tries to "optimize" or pre-evaluate URLs for gateway compatibility.
Use the fully constructed URL (with query parameters) as a single string to avoid Power BI Service manipulating the structure.
Here's how to rework your fetchPage
function safely:
fetchPage = (offset as number) =>
let
fullUrl = baseUrl & "?" & Uri.BuildQueryString([
limit = Number.ToText(limit),
offset = Number.ToText(offset)
]),
Source = Web.Contents(
fullUrl,
[
Headers = [
#"Authorization" = "Token " & token,
#"Content-Type" = "application/json"
]
]
),
JsonResponse = Json.Document(Source)
in
JsonResponse
This quirk isn't very well-documented, but it's been raised in several forums and GitHub issues, especially around:
Relative path handling
Base URL and privacy levels
URL encoding and trailing slash behavior
So yes — it's expected behavior but under-documented. You’re not doing anything wrong; this is a subtle platform inconsistency between Desktop and Service.
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
User | Count |
---|---|
47 | |
32 | |
30 | |
27 | |
25 |
User | Count |
---|---|
56 | |
55 | |
36 | |
33 | |
28 |