cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JacobBradbrook
Regular Visitor

Getting Data From POST API with JSON Body

Hello,

 

I'm very new to working with Power Query when it comes to integrating with APIs. We have a platform that we use for which we're trying to get data from their API.

 

I've looked at some other posts on the community and the only that I've found that seems similar to my situation is the one below, however I have tried both the solutions on there and they result in the same issue.

 

https://community.powerbi.com/t5/Power-Query/Getting-data-from-API-with-JSON-body/m-p/1030682

 

I've put together a query using the same format as suggested on that post however I received the 'Web.Contents failed to get contents from 'URL' (400)' error. The query I've written is below:

 

 

 

let
 url = "URL",
    body = "{""since"": ""2021-01-01"",
            ""until"": ""2021-05-10"",
            ""channels"": [""200620789980139""],
            ""metrics"": [""page_impressions"",""page_engagement""],
            ""aggregation"": {
                ""unit"": ""day"",
                ""times"": 1,
                ""aligned"": false,
                ""firstdayofweek"": 1,
                ""function"": ""default""
                }
                }",
    Source = Json.Document(Web.Contents(url,[
        Headers = [#"Content-Type"="application/json"],
        Content = Text.ToBinary(body)
    ]))
in
    Source

 

 

The URL has to include the api key for authorisation and is included in this. I've also tried the same body content and URL on a website called reqbin and this brought up results when the call was made.

 

If you could please assist with how I can amend this query to get the data it would be a massive help. As I say, this is my first time working with APIs as a whole and the first time doing so in Power Query as well so much appreciated.

1 ACCEPTED SOLUTION
V-pazhen-msft
Community Support
Community Support

@JacobBradbrook 

I see no problem with code, the 400 bad request can be caused by different reason. It is hard to identify in this one. How to Fix a 400 Bad Request Error [Causes & Fixes] (kinsta.com)

 

There is a similar blog providing details about how to connect to REST API via OAuth2 in Power BI, you can following the instructions in this blog to connect to your data source.

If you still get error, you can use Fiddler to track like the post owner did in the link you provided.

Fiddler (Web Tracing) - YouTube

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.


If you still get error, you can use Fiddler to track the process following the guide in this video.

View solution in original post

1 REPLY 1
V-pazhen-msft
Community Support
Community Support

@JacobBradbrook 

I see no problem with code, the 400 bad request can be caused by different reason. It is hard to identify in this one. How to Fix a 400 Bad Request Error [Causes & Fixes] (kinsta.com)

 

There is a similar blog providing details about how to connect to REST API via OAuth2 in Power BI, you can following the instructions in this blog to connect to your data source.

If you still get error, you can use Fiddler to track like the post owner did in the link you provided.

Fiddler (Web Tracing) - YouTube

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.


If you still get error, you can use Fiddler to track the process following the guide in this video.

View solution in original post

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Get Ready for Power BI Dev Camp

Power BI Dev Camp - June 24th

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors