cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ericOnline Member
Member

API Call Help

Hello,

 

I'm trying to use the Advanced Editor to make an API call for the first time. I've watched Chris Webb's excellent video on the subject and took a bunch of notes, but i'm missing something here. In the video, I noted his call as:

let
    Source = Web.Contents("https://baseURL",
        [
            RelativePath="hello",
            Query = [
                key = "value1"
                key = "value2"
            ],
            ApiKeyName = "apikeyskdjf;asdkfj]"])
        ]
        )
in
    Source

But my query parameters didn't like this format.

 

The call I'm trying to make is a GET request: 

let
    Source = Web.Contents(
        "https://somecompanysAPI.com/v2/",
            [
                RelativePath="historical",
                    Query = 
    {
  "geocode": {
    "lon": -122.31177520752,
    "lat": 47.4498901367188
  },
  "location_id": "",
  "start_time": "2017-11-01T12:00:00Z",
  "end_time": "2017-11-07T12:00:00Z",
  "timestep": 60,
  "fields": [
        {"name": "temp", "units": "F"},
        {"name": "feels_like", "units": "F"},
        {"name": "dewpoint", "units": "F"},
        {"name": "wind_speed", "units": "mph"},
        {"name": "wind_gust", "units": "mph"},
        {"name": "baro_pressure", "units": "inHg"},
        {"name": "visibility", "units": "km"},
        {"name": "precipitation", "units": "in/hr"},
        {"name": "cloud_cover", "units": "%"},
        {"name": "cloud_ceiling", "units": "ft"},
        {"name": "cloud_base","units": "ft"},
        {"name": "humidity", "units": "%"},
        {"name": "wind_direction", "units": "degrees"},
        {"name": "precipitation_type"},
        {"name": "sunrise"},
        {"name": "sunset"}
  ]
}
                ApiKeyName = "thisisnotreallymyAPIkeydude"
            ]
    )

in
    Source

Can someone with more experience see what I'm missing here as far as syntax goes?

 

Thank yoU!

1 ACCEPTED SOLUTION

Accepted Solutions
ericOnline Member
Member

Re: POST API request using JSON body: HELP

Ok! I figured this out from the great example in the Forums found here: https://community.powerbi.com/t5/Desktop/Connect-to-a-Web-Service-sending-parameters/m-p/295990#M130.... Thank you @emudria!

 

Here is what I ended up going with: 

let
    url = "https://company.com/api/path",
    headers = [
        #"apikey" = "thisisnotreallymyapikey",
        #"accept" = "application/json",
        #"Content-Type" = "application/json"
    ],
    content = "{
        ""key"": {
                    ""nestedKey"": value,
                    ""nestedKey"": value
                },
                ""key"": """",
                ""key"": ""value"",
                ""key"": ""value"",
                ""key"": value,
                ""key"": [
                ]
    }",
    webdata = Web.Contents(url, [Headers=headers,Content = Text.ToBinary(content)]),
    response = Json.Document(webdata)
in
    response

Whew! I should have searched the forums better! NOTED Smiley Happy

View solution in original post

2 REPLIES 2
Highlighted
ericOnline Member
Member

Re: POST API request using JSON body: HELP

Still needing some assistance here. I've boiled down the code a bit to make it easier to read. Can you see why this is not working?

 

I should be more clear here on what I'm looking for help with:


Method: POST

Authentication: API Key sent in Request Header

Parameterized Request?: No, JSON request, not a parameterized URL
Body: JSON

Request Headers?: Yes, 3 (apikey, accept, Content-Type)

Response: JSON

Response Headers?: Yes, 11 (though I don't think I need to craft the response headers, they should auto)

Error in Advanced Editor: "Invalid Identifier" error on the word "geocode".

 

I've tried "Query" and "Content" in the below M with no success. The docs on Web.Contents have NO examples (and the Github contribute link is broken on that site!). For some reason, not many people are making POST requests with PowerBI so there are like NO examples of this on the internet besides Chris Webb's video. But I can't glean how to do it from his video (@ ~24:54 mark) because he digresses a bit at EXACTLY the moment he covers POST requests!

 

let
    Source = Web.Contents(
        "https://somecompanysAPI.com/v2/historical",
            [
                Query = [
                    "geocode": {
                        "lon": -122.31177520752,
                        "lat": 47.4498901367188
                    },
                    "location_id": "",
                    "start_time": "2017-11-01T12:00:00Z",
                    "end_time": "2017-11-07T12:00:00Z",
                    "timestep": 60,
                    "fields": [
                    ],
                ApiKeyName = "thisisnotreallymyAPIkeydude"
            ]
    )

in
    Source

 Does anyone have insights on this? An example or two of how you do it?


Thank you very much!

ericOnline Member
Member

Re: POST API request using JSON body: HELP

Ok! I figured this out from the great example in the Forums found here: https://community.powerbi.com/t5/Desktop/Connect-to-a-Web-Service-sending-parameters/m-p/295990#M130.... Thank you @emudria!

 

Here is what I ended up going with: 

let
    url = "https://company.com/api/path",
    headers = [
        #"apikey" = "thisisnotreallymyapikey",
        #"accept" = "application/json",
        #"Content-Type" = "application/json"
    ],
    content = "{
        ""key"": {
                    ""nestedKey"": value,
                    ""nestedKey"": value
                },
                ""key"": """",
                ""key"": ""value"",
                ""key"": ""value"",
                ""key"": value,
                ""key"": [
                ]
    }",
    webdata = Web.Contents(url, [Headers=headers,Content = Text.ToBinary(content)]),
    response = Json.Document(webdata)
in
    response

Whew! I should have searched the forums better! NOTED Smiley Happy

View solution in original post

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Users Online
Currently online: 47 members 847 guests
Please welcome our newest community members: