Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

Unable to send POST request in Web.Contents

Hi,

I need to retrieve employee data from an API that's only accessible to authorized users. First, I send a POST request using client credentials to obtain a bearer token and then it can access the employee API with this token. This process works smoothly on Power BI Desktop. However, once I publish the report to Power BI Service, the access token request becomes a GET request, resulting in a "Method Not Allowed" error with response code 405. 

 

This is the error I have encountered.

 

 

Failed to update data source credentials: Web.Contents failed to get contents from 'https://a95a-123-231-56-85.in.ngrok.io/oauth/issueToken' (405): Method Not AllowedHide details
Activity ID:	56d0cb6a-3c96-4b10-8ae5-1de4fc1cd8e8
Request ID:	43940e4e-6384-e9ab-7d05-54e2ad842885
Status code:	400
Time:	Thu Mar 09 2023 15:35:33 GMT+0530 (India Standard Time)
Service version:	13.0.20195.63
Client version:	2302.4.12605-train
Cluster URI:	https://wabi-uk-south-c-primary-redirect.analysis.windows.net/

 

 

 

This is the Power Query code

 

 

let
    baseUrl = [base_url],
    client_secret = [client_secret],
    Source  = Json.Document(Web.Contents(baseUrl&"/oauth/issueToken",
        [ 
          Headers = [#"Content-Type"="application/json"],
          Content=Json.FromValue([
            grant_type = "client_credentials",
            client_id = "powerbi_client",
            client_secret = client_secret
          ])
        ]
    )),

    token = Source[access_token],

    data= Json.Document(Web.Contents(baseUrl&"/api/employees",
      [ 
        Headers = [#"Authorization"="Bearer "&token]
      ]
    )),
    data1 = data[data],
    #"Converted to Table" = Table.FromList(data1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"empNumber", "firstName", "lastName", "middleName", "termination_id", "employeeId", "joinedDate", "deletedEmployee"}, {"empNumber", "firstName", "lastName", "middleName", "termination_id", "employeeId", "joinedDate", "deletedEmployee"})
in
    #"Expanded Column1"

 

 

 

According to the documentation, "Content" specifies that the web request should be changed from a GET to a POST, with the value of the option being used as the content of the POST request. As I have mentioned earlier, this is working fine in Power BI Desktop, but not in the Power BI Service as expected.

 

What’s the issue here? Any issue with my script?