<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.15">
  <POU Name="FB_TestHTTP_Put" Id="{0c2eb22b-3429-488d-9d6d-7cf613b59727}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_TestHTTP_Put
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;
	fbJson				: FB_JsonDomParser;
	nState				: UDINT;
	RisingEdge			: R_TRIG;
	
	bGetContentResult	: BOOL;
	sContent			: STRING(511);
	
	bGetJsonResult		: BOOL;
	jsonDoc				: SJsonValue;
	jsonVal				: SJsonValue;
	sResultValue		: STRING;
		
	nReqCount			: UDINT;
	nResCount			: UDINT;
	nValidResCount		: UDINT;
	nErrCount			: UDINT;	
END_VAR	]]></Declaration>
    <Implementation>
      <ST><![CDATA[RisingEdge(CLK:= bSend);
CASE nState OF
0:		
	IF RisingEdge.Q THEN 
		IF fbRequest.SendRequest(sUri:='/test_put', fbClient:= fbClient, eRequestType:= ETcIotHttpRequestType.HTTP_PUT, pContent:= ADR(sContent), nContentSize:= LEN2(ADR(sContent)), 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, 'response')  THEN
						jsonVal:= fbJson.FindMember(jsonDoc, 'response');
							nValidResCount:= nValidResCount+1;
							bError:= FALSE;						
							IF fbJson.IsString(jsonVal) THEN							
								sResultValue:= fbJson.GetString(jsonVal);									
							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
]]></ST>
    </Implementation>
  </POU>
</TcPlcObject>