Forum Discussion

TuanPham1999's avatar
TuanPham1999
Frequent Visitor
7 years ago
Solved

Pull data from API using POST method in Power BI

Hi all, I'm a fresher in Power BI Desktop. my mission is extract data from given credentials include: ClientID, Password and Url. The credentials must be Base64 encoded for use in the POST /oauth2/token request. I have used that credentials in Postman and I got its header such as: Authorization: Basic xxxxxxxxxxxxxxxxxxxxxx. what should i start with all of them to get data from API. Can anyone help me to solve this probleml. thank you so much for any solution and suggestion. Cheer!!!

7 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    I found it not so easy to get data via POST so I am pasting here what I did in case this helps someone else in the future.

     

    I created the following blank query:

     

    = let
       body = "The POST method body here",
       Data= Web.Contents("https://yourusrlhere",[Content=Text.ToBinary(body),Headers=[#"Content- 
       Type"="application/json"]]),
       DataRecord = Json.Document(Data),
       Source=DataRecord 
    in
       Source

     

     

    • NateDogg's avatar
      NateDogg
      New Member

      I wanted to post a more complete/cleaner version of the above solution that contains multiple headers and shows how to convert the body to binary data for POST operations: 

       

      let
          url = "https://api.somevendor.com/api/",
          headers = [#"Content-Type" = "application/x-www-form-urlencoded", #"Accept" = "application/json"],
          postData = "Any sort of text data you want to post to the API",
          response = Web.Contents(
              url,
              [
                  Headers = headers,
                  Content = Text.ToBinary(postData)
              ]
          ),
          jsonResponse = Json.Document(response)
      in
          jsonResponse

       

    • Markzolotoy's avatar
      Markzolotoy
      Impactful Individual

      What  body = "The POST method body here", is for?

       

      Thanks

    • i4's avatar
      i4
      Frequent Visitor

      How can I insert a variable (dateTo) instead of the date, i.e. 2024-12-31, into parameter5?

       

      let
         dateTo= DateTime.LocalNow(),
         body = "{""parameter1"":""xxx"",""parameter2"":""xxx"",""parameter3"":""xxx"",""parameter4"":""xxx"",""parameter5"":""2024-12-31""}",
         Data= Web.Contents("https://xxx",[Content=Text.ToBinary(body),Headers=[#"Content-Type"="application/json"]]),
         DataRecord = Json.Document(Data),
         Source=DataRecord
      in
         Source

       

  • TuanPham1999's avatar
    TuanPham1999
    Frequent Visitor

    Hi all, I'm a fresher in Power BI Desktop. my mission is extract data from given credentials include: ClientID, Password and Url. The credentials must be Base64 encoded for use in the POST /oauth2/token request. I have used that credentials in Postman and I got its header such as: Authorization: Basic xxxxxxxxxxxxxxxxxxxxxx. what should i start with all of them to get data from API. Can anyone help me to solve this probleml. thank you so much for any solution and suggestion. Cheer!!!

  • Thiaan's avatar
    Thiaan
    Regular Visitor

    if you add content ie:

     Content = Text.ToBinary(postData)

     to the web.conents on the call it automatically does a post. just send a parameter to the API ie: 

    Content = Json.FromValue([ pageSize = "200" ]), //even though this MIGHT be the same as the default, specifying content forces the post. 

    finally:

    ...
    Web.Contents( Url

                                  , [ Headers = Headers
                                  , Content = Json.FromValue([ pageSize = "200" ]) // ← This makes it a POST
    ...