Forum Discussion
Access data from POST API
- Anonymous5 years ago
HI Anonymous,
>>DataSource.Error: The downloaded data is HTML, which isn't the expected type. The URL may be wrong or you might not have provided the right credentials to the server.
Perhaps this issue appears when you try to use the wrong connector to recognize response data.
I'd like to suggest you navigation to the 'Source' step to confirm the result type and change to the corresponding connector in the next steps to recognize/analytics records.Regards,
Xiaoxin Sheng
HI Anonymous,
You can take a look at the following link about using a web connector to get data from the rest API with authorizations:
Pull data from a REST API Authentication
Sample code:
let
token = 'your token',
body= 'request body',
url = "https://clientwebsitename.azurewebsites.net/",
Source = Web.Contents(url,
[
Headers = [#"X-Token"=token],
RelativePath="dev/masterdata/departments/get",
Content = Text.ToBinary(body)
]
)
in
Source
Regards,
Xiaoxin Sheng
- Anonymous5 years agoNot applicable
Hi Anonymous,
Thank You for the reply, I have tried the code you have provided but it gives the following error-
Expression.Error: The 'X-Token' header is only supported when connecting anonymously. These headers can be used with all authentication types: Accept, Accept-Charset, Accept-Encoding, Accept-Language, Cache-Control, Content-Type, If-Modified-Since, Prefer, Range, Referer.
I have tried in postman and it works, what process I have used in postman as below-
1. I have URL to get Token: (jwt)
URL: https://Clientwebsite.azurewebsites.net/dev/auth/user/authenticate
here I have provided the payload as well
2. Then another URL to get the Data
URL: https://Clientwebsite.azurewebsites.net/dev/masterdata/departments/get
here I have provided Kayname (X-Token) and jwd Token(generated from 1st step).
Please help me to retrieve the data.
Thanks
- Anonymous5 years agoNot applicable
Hi Anonymous,
It seems like you need to add the first strep into the query table to get the access token and use in the following steps, I try to modify the formula to add the first step into the formula:
let url = "https://clientwebsitename.azurewebsites.net/", // Uses the Twitter POST oauth2/token method to obtain a bearer token GetJson = Web.Contents(url, [ Headers = [#"Authorization"=authKey, #"Content-Type"="application/json"],//use the sent parameters and name as you do in postman to replace current Authorization RelativePath="dev/auth/user/authenticate" ] ), token = Json.Document(GetJson)['token name'],//use tokne name to repalce 'tokne name' part to extract the responsed token Source = Web.Contents(url, [ Headers = [#"X-Token"=token, #"Content-Type"="application/json"], RelativePath="dev/masterdata/departments/get" ] ), Result=Json.Document(Source) in ResultBTW, how did you post the authorizations in your requests? In the header, body or concatenate into query string? My formula is add authorized into the headers and it may not suitable for all scenarios.
Chris Webb's BI Blog
Regards,XIaoxin Sheng
- Anonymous5 years agoNot applicable
Hi Anonymous,
Thank you for the suggestion, But it gives the error,
What I have tried:
let
url = "https://clientwebsitename.azurewebsites.net/",
body = "{
""grant_type"": ""client_credentials"",
""TenantId"": ""Client2"",
""UserSignon"": ""corp2019"",
""Password"": ""test""
}",
GetJson = Web.Contents(url,
[
Headers = [#"Content-Type"="application/raw"],
Content=Text.ToBinary(body),//use the sent parameters and name as you do in postman to replace current Authorization
RelativePath="dev/auth/user/authenticate"
]
),
token = Json.Document(GetJson)['jwt'],
Source = Web.Contents(url,
[
Headers = [#"X-Token"=token, #"Content-Type"="application/json"],
RelativePath="dev/masterdata/departments/get"
]
),
Result=Json.Document(Source)
in
ResultThe Query is run successfully when suppressed the ['jwt'] from 2nd query and It generated the 2 Token with Value as below
The problem is ['jwt] is not worked, it gives error as" Invalid Identifier".
Thanks