Forum Discussion
POST and GET request via Web API. POST request is succesful, but can't perform the GET request
If you know for sure that the second GET will work then you could call the GET twice, and use the result from the second call.
Of course it would be good to understand why exactly the first GET fails.
- rwaanders5 years agoFrequent Visitor
Thanks for you reply, I now understand why the first GET fails.
The reason is that my first POST request triggers the creation of the file.
Once the file is generated, i can perform the GET request to retrieve the file.
That's why on the second attempt, 9 out of 10 times I can get the file.
What I tried is build in a InvokeAfter function with a loop of 10 x 5 seconds (50 seconds in total), and return the status (this is a separate GET request I read in the API documentation yesterday). Once one of the rows returns "success", then the query should proceed with the original GET request to retrieve the file.
Unfortunately I keep getting the status "running", no matter if I say to run it 10 times for 5 seconds or 10 times for 1 second. But when I try the link in my browser, the status is already on "success". It looks like something is going wrong with the InvokeAfter function. My intention is that after i did the POST request, the status gets retrieved after 5 - 10 - 15 until 50 seconds. Once one of these result as "success", I can proceed to download the file.
See code:
let
//POST request
base_url = "https://*****.nl/api/dataviewresult/"&Dataview&"/json?",
extension = "api_key=*****",
url = base_url &extension,
header= [#"Content-Type" = "application/json"],
content1= "{}",
webdata1 = Web.Contents(url, [Headers=header,Content = Text.ToBinary(content1)]),
response1 = Json.Document(webdata1),
contents = response1[contents],
id = contents[id],// WAITING table
WaitForList = List.Repeat({5}, 10),
#"Converted to Table" = Table.FromList(WaitForList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),// GET the status 10x after 5 seconds
#"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each Function.InvokeAfter(
() => Json.Document(Web.Contents("https://*****/api/dataviewresult/" & Dataview & "/json/" & id & "/status?api_key=*****"))[contents][status],
#duration(0,0,0,[Column1]))),//Add GET URL to check whether the ID/Dataview ID are correct
#"Added Custom2" = Table.AddColumn(#"Added Custom", "Custom2", each Json.Document(Web.Contents("https://*****/api/dataviewresult/" & Dataview & "/json/" & id & "/status?api_key=*****"))[contents][url]),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom2",{{"Custom", type text}}),// wait until the response is COMPLETE, but max 50 secs
WaitForStatus = Table.MatchesAnyRows(#"Changed Type", each [Custom] = "success"),
// download file if WaitForStatus was successful
DownloadFile =
if WaitForStatus then
Json.Document(Web.Contents("https://*****/api/dataviewresult/" & Dataview & "/json/" & id & "?api_key=*****"))
else
nullin
DownloadFile