The ultimate Microsoft Fabric, Power BI, Azure AI, and SQL learning event: Join us in Stockholm, September 24-27, 2024.
Save €200 with code MSCUST on top of early bird pricing!
Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
I am attempting to send a POST request using an API in PowerQuery on Excel/PBI. The request requires a body, and I've tried a few different ways (see below) but I keep receiving a DataSource.Error: Web.Contents failed to get contents from 'https://myapi.com/data' (400): Bad Request.
My request looks like this:
let
Source = Json.Document(Web.Contents("https://myapi.com/data", [Headers=[#"X-Impersonate-User"="usr_12345", Authorization="Bearer tok_12345", #"Content-Type"="application/json"],
Content=Json.FromValue({[start_date="2022-08-01T08:00:00.000Z", end_date="2022-08-10T08:00:00.000Z", space_ids="spc_12345", time_resolution="day"]})
]))
in
Source
I also tried it this way, but ran into the same issue:
let
url = "https://myapi.com/data"
body = "{""start_date"" : ""2022-08-01T08:00:00.000Z"", ""end_date"" : ""2022-08-10T08:00:00.000Z"", ""space_ids"" : ""spc_12345"", ""time_resolution"" : ""day""}",
Parsed_JSON = Json.Document(body),
BuildQueryString = Uri.BuildQueryString(Parsed_JSON),
Source = Json.Document(Web.Contents(url,[Headers=[#"Content-Type"="application/json", #"X-Impersonate-User"="usr_12345", Authorization="Bearer Bearer tok_12345"], Content = Text.ToBinary(BuildQueryString) ] ))
in
Source
Tried this too... In this last one it says "Please specify how to connect." but does not let me select public even though I hard coded the API Key.
let
auth_key = "Bearer MyApiKey",
url = "https://public-api.mycompany.com/analytics",
header = [#"X-Impersonate-User"="UserKeyHere", Authorization=auth_key, #"Content-Type"="application/json"],
content = "{
""start_date"":""2022-08-01T08:00:00.000Z"",
""end_date"":""2022-08-10T08:00:00.000Z"",
""space_ids"":[""12345""],
""time_resolution"":""day"",
}",
webdata = Web.Contents(url, [Headers=header,Content = Text.ToBinary(content)]),
response = Json.Document(webdata)
in
response
I am able to call the API from Postman with no problems.
Solved! Go to Solution.
For anyone who it might help, I successfully solved this. Here is my code:
let
url = "https://myapi.com/analytics",
headers = [#"Content-Type"="application/json", #"X-Impersonate-User"=Impersonation_Token, Authorization="Bearer "&API_Key],
body = "{""start_date"":""" & Start_Date &""",""end_date"":""" &End_Date &""",""time_resolution"":""" & Time_Dimension &"""}",
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary((body))
])),
in
For anyone who it might help, I successfully solved this. Here is my code:
let
url = "https://myapi.com/analytics",
headers = [#"Content-Type"="application/json", #"X-Impersonate-User"=Impersonation_Token, Authorization="Bearer "&API_Key],
body = "{""start_date"":""" & Start_Date &""",""end_date"":""" &End_Date &""",""time_resolution"":""" & Time_Dimension &"""}",
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary((body))
])),
in
Hi @Anonymous
Please try below code
let
url = "https://myapi.com/data",
headers = [#"Content-Type"="application/json", #"X-Impersonate-User"="usr_12345", Authorization="Bearer xxxxxxxxxxxxxx"],
body = [
start_date = "2022-08-01T08:00:00.000Z",
end_date = "2022-08-10T08:00:00.000Z",
space_ids = "spc_12345",
time_resolution = "day"
],
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary(Uri.BuildQueryString(body))
]))
in
response
Then it will pop up "Please specify how to connect." Click "Edit Credentials" and select Anonymous type.
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.
When I try that and click anonymous, I receive this error back from BI.
Hi @Anonymous
Can you please capture screenshots of the POST request settings that is successful in Postman? Remember to remove sensitive data from them. I'd like to see if we have missed some settings in the query.
Best Regards,
Community Support Team _ Jing
Here are some screen-grabs from Postman.
Hi @Anonymous
I made a small modification. Not sure if this works.
let
url = "https://myapi.com/data",
headers = [#"Content-Type"="application/json", #"X-Impersonate-User"="usr_12345", Authorization="Bearer xxxxxxxxxxxxxx"],
body = [
start_date = "2022-08-01T08:00:00.000Z",
end_date = "2022-08-10T08:00:00.000Z",
space_ids = {"spc_12345", "spc_1234567"},
time_resolution = "day"
],
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary(Uri.BuildQueryString(body))
]))
in
response
Best Regards,
Community Support Team _ Jing
Unfortunately got the same "(400): Bad Request" error.
Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.
Check out the August 2024 Power BI update to learn about new features.