The function block is started by writing the variable bGet in the main program to TRUE.
The rising edge is then used to send a GET request with the IotHttpRequest function block.
After the request has been finished, the error handling is processed. When neither the function block itself nor the HTTP status code display an error, the JSON response from the webserver is parsed.
RisingEdge(CLK:= bSend); CASE nState OF 0: IF RisingEdge.Q THEN IF fbRequest.SendRequest(sUri:= '/get?foo1=bar1&foo2=bar2', fbClient:= fbClient, eRequestType:= ETcIotHttpRequestType.HTTP_GET, 0, 0, 0) THEN nState:= 1; nReqCount:= nReqCount+1; bBusy:= TRUE; bError:= FALSE; END_IF END_IF 1: IF NOT fbRequest.bBusy THEN bError:= TRUE; IF NOT fbRequest.bError THEN bGetContentResult:= fbRequest.GetContent(pContent:= ADR(sContent), nContentSize:= SIZEOF(sContent), bSetNullTermination:= TRUE); IF fbRequest.nStatusCode >= 200 AND fbRequest.nStatusCode < 300 THEN bGetJsonResult:= FALSE; jsonDoc:= fbRequest.GetJsonDomContent(fbJson); IF jsonDoc <>0 THEN bGetJsonResult:= TRUE; IF fbJson.HasMember(jsonDoc, 'args') THEN jsonVal:= fbJson.FindMember(jsonDoc, 'args'); IF fbJson.HasMember(jsonVal, 'foo2') THEN jsonVal:= fbJson.FindMember(jsonVal, 'foo2'); nValidResCount:= nValidResCount+1; bError:= FALSE; IF fbJson.IsString(jsonVal) THEN sResultValue:= fbJson.GetString(jsonVal); END_IF END_IF END_IF END_IF nResCount:= nResCount+1; END_IF END_IF nState:= 0; bBusy:= FALSE; IF bError THEN nErrCount:= nErrCount+1; END_IF END_IF END_CASE