Header Authentication

  1. The function block is started by writing the variable bHeaderAuth in the main program to TRUE.
  2. The rising edge is then used to send a GET request with the IotHttpRequest function block. This request contains an additional header field for authentication.
  3. 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. The JSON response contains the information if the authentication has been successful.
FUNCTION_BLOCK FB_TestHTTP_HeaderAuth
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;
    fbHeader            : FB_IotHttpHeaderFieldMap;
    fbJson              : FB_JsonDomParser;
    nState              : UDINT;
    RisingEdge          : R_TRIG;
    
    bGetContentResult   : BOOL;
    sContent            : STRING(511);
    
    bGetJsonResult      : BOOL;
    jsonDoc             : SJsonValue;
    jsonVal             : SJsonValue;
    bResultValue        : BOOL;
    
    nReqCount           : UDINT;
    nResCount           : UDINT;
    nValidResCount      : UDINT;
    nErrCount           : UDINT;
    bOnce               : BOOL:= TRUE;
END_VAR
IF bOnce THEN
    fbHeader.AddField('Authorization', 'Basic cG9zdG1hbjpwYXNzd29yZA==', FALSE);
    bOnce:= FALSE;
END_IF

RisingEdge(CLK:= bSend);
CASE nState OF
0:
    IF RisingEdge.Q THEN
        IF fbRequest.SendRequest(sUri:= '/basic-auth', fbClient:= fbClient, 
                                 eRequestType:= ETcIotHttpRequestType.HTTP_GET, 0, 0, fbHeader) 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, 'authenticated') THEN
                        jsonVal:= fbJson.FindMember(jsonDoc, 'authenticated');
                        IF fbJson.IsBool(jsonVal) THEN
                            bResultValue:= fbJson.GetBool(jsonVal);
                            nValidResCount:= nValidResCount+1;
                            bError:= FALSE;
                        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