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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Kartnepe
Regular Visitor

Web Contents does not return json data from web API

The Road Administration in Norway provides trafficdata in an web api using GraphQL (https://www.vegvesen.no/trafikkdata/api/). I have unsuccessfully tried to set up a connection and load data into a table in Power BI. It seems like web.contents returns html for the website rather than json data. I have run the query in Postman and there it returns correct json data.

 

appreciate some tips that can lead me the right way:-)

 

This is my code:

let
uri = "https://www.vegvesen.no/trafikkdata/api/",
headers = [#"Content-Type" = "application/json"],
query = "

query counties {

       areas {

         counties {

           number

           name

         }

       }

    }

",
source = uri & "?query=" & Uri.EscapeDataString(query),
resp = Web.Contents(source, [Headers=headers]),
json = Json.Document(resp)

in
json

1 ACCEPTED SOLUTION

Ah, never mind.  Having a look at how they actually call the API reveals that this is a POST request, which means you need to stuff the query into the payload instead.

 

 

 

let
query = "{areas{counties{number name}}}",
uri = "https://www.vegvesen.no/trafikkdata/api/",
    Source = Json.Document(Web.Contents(uri, [Headers=[#"Content-Type"="application/json"], Content=Json.FromValue([query = query])])),
    #"Converted to Table" = Table.FromList(Source[data][areas][counties], Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"number", "name"}, {"number", "name"})
    
in
    #"Expanded Column1"

 

 

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

The problem seems to be that the website is trying to be cute, and is providing a placeholder "Loading..." page first

 

lbendlin_0-1649537149169.png

 

You would have to ignore that and fetch the next page.

 

Your code is also missing an important query parameter.

 

https://www.vegvesen.no/trafikkdata/api/?query=query%20counties%7Bareas%7Bcounties%7Bnumber%20name%7D%7D%7D&operationName=counties

 

 

Here is the "proper" Power Query code that you shoud be using.

 

 

Parameters uri and query

uri = "https://www.vegvesen.no/trafikkdata/api/"
query = "query counties{areas{counties{number name}}}"

Request

Web.Contents(uri, [Headers=[Accept="application/json"], Query=[query=query,operationName="counties"]])

 

 

@ImkeF  Do you remember how to handle URI redirects in such a scenario?

Ah, never mind.  Having a look at how they actually call the API reveals that this is a POST request, which means you need to stuff the query into the payload instead.

 

 

 

let
query = "{areas{counties{number name}}}",
uri = "https://www.vegvesen.no/trafikkdata/api/",
    Source = Json.Document(Web.Contents(uri, [Headers=[#"Content-Type"="application/json"], Content=Json.FromValue([query = query])])),
    #"Converted to Table" = Table.FromList(Source[data][areas][counties], Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"number", "name"}, {"number", "name"})
    
in
    #"Expanded Column1"

 

 

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Power BI Header Change

Power BI Header Update

Read about the exciting updates for the Power BI forum.

Top Solution Authors