Get
- The function block is started by setting the variable bGetAWSSigV4 in the main program to TRUE.
- The rising edge is then used to send a GET request with the function block IotHttpRequest.
- Once the request is complete, an error handling routine is invoked. If both the function block itself and the HTTP status code are error-free, the XML response can be parsed by the web server in the PLC program (the response of the AWS REST API can become quite long, so sufficient memory should be reserved for this purpose, and it may be advisable to test the procedure with an HTTP test client).
![]() | Alphabetical sorting required The request URL must be sorted alphabetically, ditto the signed header. In the case of the signed header, this is handled by the IoT driver; in the case of the request URL, it is the responsibility of the user, otherwise an error message will be issued by the HTTP server. |
FUNCTION_BLOCK FB_TestHTTP_Get_AWSSigV4
VAR_INPUT
bSend : BOOL;
END_VAR
VAR_IN_OUT
fbClient : FB_IotHttpClient;
END_VAR
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
END_VAR
VAR
fbRequest : FB_IotHttpRequest;
fbSig4Header : FB_IotHttpAwsSigV4HeaderFieldMap;
nState : UDINT;
RisingEdge : R_TRIG;
bGetContentResult : BOOL;
sContent : STRING(5000);
bGetJsonResult : BOOL;
sResultValue : STRING;
nReqCount : UDINT;
nResCount : UDINT;
nValidResCount : UDINT;
nErrCount : UDINT;
sRequestUrl : STRING(500):='?Action=RunInstances&ImageId=ami-0229f7666f517b31e&InstanceType=t2.small&KeyName=TestKDB&MaxCount=1&MinCount=1&Version=2016-11-15';
//sRequestUrl : STRING(500):='?Action=DescribeInstances&Version=2016-11-15';
sService : STRING:='ec2';
sRegion : STRING:='us-east-1';
sAccessKey : STRING:='censored';
sSecretKey : STRING:='censored';
sSignedHeaders : STRING:='host;x-amz-date';
bSetParameter : BOOL;
END_VAR
RisingEdge(CLK:= bSend );
CASE nState OF
0:
IF RisingEdge.Q THEN
bSetParameter:=fbSig4Header.SetParameter(sService, sRegion, sAccessKey, sSecretKey, sSignedHeaders);
IF fbRequest.SendRequest(sUri:=sConMessage, fbClient:=fbClient, eRequestType:=ETcIotHttpRequestType.HTTP_Get, 0, 0, fbHeader:=fbSig4Header) THEN
nState:= 1;
nReqCount:= nReqCount+1;
bBusy:= TRUE;
bError:= FALSE;
END_IF
bSetParameter:=FALSE;
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
nResCount:= nResCount+1;
// do something with the XML response
bError:=FALSE;
END_IF
END_IF
nState:= 0;
bBusy:= FALSE;
IF bError THEN
nErrCount:= nErrCount+1;
END_IF
END_IF
END_CASE