Forum Discussion
Post request to API does not recognise the posted payload as Json (see python code below)
- 6 years ago
Hi Anonymous
no need to use the advanced editor. Paste it into the formula bar, but make sure that the "=" is replaced as well. Then you're getting valid JSON (with my code) that can be parsed as JSON.
The reason why you're seeing this trash is because your code is being parsed as Csv.Document (see formula in the formula bar)
let authKey = "{""grant_type"":""client_credentials"", ""client_id"":""****************************"", ""client_secret"":""*************************""}", url = "https://shv.piwik.pro", // Uses the authentication/token method to obtain a token GetJson = Json.Document(Web.Contents(url, [Headers= [#"Content-Type"="application/json"], Content=Text.ToBinary(authKey), RelativePath="/auth/token"])), AccessToken = GetJson[access_token], //// This is now valid JSON and should technically work (provided the format fits to the API body = "[{""columns"": [{ ""column_id"": ""website_name""}, { ""column_id"": ""session_entry_url""}]}, #(cr)#(lf) { ""format"": ""json""}, #(cr)#(lf) { ""date_from"": ""2020-01-01""}, #(cr)#(lf) { ""date_to"": ""2020-03-01""}, #(cr)#(lf) { ""website_id"": ""a33fcc21-7349-4013-981c-468e1231696e""} ,#(cr)#(lf) { ""offset"": ""0""}, #(cr)#(lf) { ""limit"": ""100""} #(cr)#(lf)]", data= Json.Document( Web.Contents(url, [Headers = [#"Authorization"="Bearer "&AccessToken, #"Content-Type"="application/json", #"Accept-Encoding"= "gzip"], RelativePath="/api/analytics/v1/sessions", Content = Json.FromValue(body) ])) in data
Hi Anonymous
no need to use the advanced editor. Paste it into the formula bar, but make sure that the "=" is replaced as well. Then you're getting valid JSON (with my code) that can be parsed as JSON.
The reason why you're seeing this trash is because your code is being parsed as Csv.Document (see formula in the formula bar)
let
authKey = "{""grant_type"":""client_credentials"",
""client_id"":""****************************"",
""client_secret"":""*************************""}",
url = "https://shv.piwik.pro",
// Uses the authentication/token method to obtain a token
GetJson = Json.Document(Web.Contents(url,
[Headers= [#"Content-Type"="application/json"],
Content=Text.ToBinary(authKey), RelativePath="/auth/token"])),
AccessToken = GetJson[access_token],
//// This is now valid JSON and should technically work (provided the format fits to the API
body = "[{""columns"": [{ ""column_id"": ""website_name""}, { ""column_id"": ""session_entry_url""}]}, #(cr)#(lf) { ""format"": ""json""}, #(cr)#(lf) { ""date_from"": ""2020-01-01""}, #(cr)#(lf) { ""date_to"": ""2020-03-01""}, #(cr)#(lf) { ""website_id"": ""a33fcc21-7349-4013-981c-468e1231696e""} ,#(cr)#(lf) { ""offset"": ""0""}, #(cr)#(lf) { ""limit"": ""100""} #(cr)#(lf)]",
data= Json.Document( Web.Contents(url,
[Headers = [#"Authorization"="Bearer "&AccessToken,
#"Content-Type"="application/json",
#"Accept-Encoding"= "gzip"],
RelativePath="/api/analytics/v1/sessions",
Content = Json.FromValue(body)
]))
in
data
Hi ImkeF
Thanks for your fast reply. It looks like the Json is working but I am still stuck.
I got the same question when retrieving the access token, but when I click connect it works.
Also In python it works:
Result:
I really don't understand what I am missing here.
THanks, LinV
- ImkeF6 years agoCommunity Champion
Hi Anonymous
hard to tell. Only thing that comes to my mind is: Try to omit the leading "/" in the relative path:
RelativePath="api/analytics/v1/sessions",
instead of:
RelativePath="/api/analytics/v1/sessions",