I must connect a PowerBI report to a MongoDB source and I must use MongoDB APIs. I made a post API call in Postman and I got all MongoDB collections I requested. So I made a query in PowerQuery using the Web connector with the same API call I used in Postman, but it returns Bad Request. This is the query:
let
url = "https://eu-central-1.aws.data.mongodb-api.com/app/data-datwc/endpoint/data/v1/action/find",
headers = [#"Content-Type"="application/json", #"api-key"="{{MYAPIKEY}}"],
body = [dataSource= "MyCluster",
database= "sample_airbnb",
collection= "listingsAndReviews"],
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary(Uri.BuildQueryString(body))
]))
in
response
I'm using Anonymous permission settings with Public privacy level. My API call appears to be identical in Power Query and in Postman, so why does it work in Postman and not in Power Query?
Well, after long investigations I found the solution. I had to replace this:
Content = Text.ToBinary(Uri.BuildQueryString(body))
with this:
Content = Json.FromValue(body)
All online resources gave the first solution, but it was wrong as the Text.ToBinary generates a wrongly formatted body in the request.