Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

power query Post request to Web API

Dear Community,

I am trying to connect Power BI with Web Api.

 

This one is working.

let
    Source = Json.Document(Web.Contents("http://api-pub.dudooeat.com/auth/getAccessToken", [Headers=[#"X-Api-Key"="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx755"]]))
in
    Source

 

I try to post the query, the API show error

let
    refreshAccessToken=Json.Document(Web.Contents("http://api-pub.dudooeat.com/auth/getAccessToken", [Headers=[#"X-Api-Key"="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx755"]])),
    body="scope=all",
    Source = try Json.Document(Web.Contents("http://api-pub.dudooeat.com/sales/listCompanyId",[Headers=[#"Access-Token-Api"=refreshAccessToken,#"Content-type"="application/x-www-form-urlencoded",#"Content-Length"="6"],Content=Text.ToBinary(body)]))
in
    Source

 Is there any error in my query? 😞

7 Replies

  • Hi Anonymous 

    That code is syntactically ok.

    What's the error you are getting?

    Phil

    • Anonymous's avatar
      Anonymous
      Not applicable

      PhilipTreacy 
      It shows Expression.Error,but I don't know where is the problem

  • Hi Anonymous 

    Not enough info to know how to help.  The error message willsay more than just Expression.Error - what's the full error message?

    Regards

    Phil

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi PhilipTreacy 

      Sorry I did not pay attention.

      Put a photo to complete the problem.

      Thank you for your help.

  • Hi Anonymous 

    Your first step is returning a Record, not text. Click on the refreshAccessToken step andyou'll see what I mean.

    So the body parameter is being set to a Record and this is throwing the error.

     

    I can't tell you what's in the Record (its structure) as I don't actually have access to the API.  But as you are trying to get the AccessToken I imagine the record will have a field in it named something like Token.  You can check by clicking beside the Record as you did to see the error message.

     

    So assuming the Access Token is in a field called Token you need to change the Headers in your second web request to 

     

     

    #"Access-Token-Api"=refreshAccessToken[Token]

     

     

    Just match the field name from the record in the Header.

    Let me know how you get on.

    Phil


    If I answered your question please mark my post as the solution.
    If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, PhilipTreacy 

      I try to avoid the error and use this methods

      But found another problem

      When I'm in Query Editor I can see the data tables with zero issues, but every single time when I try to do Close & Apply it failed

       

      Best regards

  • Hi Anonymous 

    In your 3rd step you have

     

    data = Source1[data]

     

     

    But Source1 (the JSON) doesn't contain a record named data - that's what's causing your error.

     

    For debugging purposes remove all steps after Source1 = ..... and make sure to remove the comma at the end of the Source1 step too.  You also need to tell the query what value is the result.

    You should end up with something like this

     

    let
       body = .......
       Source1 = ......
    in
       Source1

     

     

    You can now check what Source1 is actually returning and go from there.

    Regards

    Phil


    If I answered your question please mark my post as the solution.
    If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.