Problem with WebClient PUT (400 Bad Request) command FB from CODESYS IIoT libraries

I do some tests with the WebClient FB's from CODESYS IIoT libraries. In this tests I interact with the ctrlX CORE itself. I'm able to read (GET) data and also to get (POST) the Bearer token:

GET to get the type plate from CORE (sURL:= 'https://127.0.0.1/system/api/v1/typeplate')

POST to get the Bearer token (sURL:= 'https://127.0.0.1/identity-manager/api/v1/auth/token')

But I'm not able to write (PUT) data. I would like to write a datalayer variable, but I get always the error 400 Bad Request:

This is my declaration: 

wsPOSTCommand : WSTRING(511);
wsBEARERToken : WSTRING(1024);
fbWebClientPUT: WEB_CLIENT.WebClient;

 

This is my code:

wsPOSTCommand := "{$"type$": $"uint32$", $"value$": 99}";
wsBEARERToken := "{$"Authorization$": $"Bearer eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTIxODc1ODYsImlhdCI6MTcxMjE1ODc4NiwiaWQiOiIxMDAwIiwibmFtZSI6ImJvc2NocmV4cm90aCIsIm5vbmNlIjoiMTBjNjRhNTUtNmQxZC00NjU3LTk1ZWEtMWY1ZDY4MmNkODU3IiwicGxjaGFuZGxlIjozMTY5NDc3OTA1MjM0NTE3MjYsInJlbW90ZWF1dGgiOiIiLCJzY29wZSI6WyJyZXhyb3RoLWRldmljZS5hbGwucnd4Il19.CcZ1xZnvTRY0r3f-rjJLo7BEAGJif9wDlNHYO7USYUboZmMlLkjrgI6zuA2J8XAg1CuB8wtEAOLQRZLXjLlQbyJICF5wD6eE8ff4Xu59A149hBKC6m77JD82FPkRHDNH$"}";


// try to use a PUT command
fbWebClientPUT(
	xExecute			:= , 
	udiTimeOut			:= 1000000, 
	sURL				:= 'https://127.0.0.1/automation/api/v2/nodes/plc/app/Application/sym/GVL_Datalayer/dwTestDWORD', 
	eRequestType		:= WEB_CLIENT.REQUEST_TYPE.PUT,
	eContentType		:= WEB_CLIENT.CONTENT_TYPE.APPLICATION_JSON,
	pwsAdditionalHeader	:= ADR(wsBEARERToken), 
	pwsPostValue		:= ADR(wsPOSTCommand), 
	xCloseConnection	:= , 
	hCert				:= , 
	itfTLSContext		:= , 
	itfAsyncProperty	:= , 
	xDone				=> , 
	xBusy				=> , 
	xError				=> , 
	eError				=> , 
	httpResult			=> );

 

 

 

Best reply by LunatiX

Thanks for the hint. I found the problem and now the test was sussessfull with the code below. The problem was the syntax of my variable wsBEARERToken. 

Code which works for me:

wsPOSTCommand := "{$"type$": $"uint32$", $"value$": 99}";
wsBEARERToken := "Authorization: Bearer eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTIyNjM3NjMsImlhdCI6MTcxMjIzNDk2MywiaWQiOiIxMDAwIiwibmFtZSI6ImJvc2NocmV4cm90aCIsIm5vbmNlIjoiYjM0YWEyYjEtMWE1NC00YzU5LWJjMDktNzlmZDA3ZTRkZjE2IiwicGxjaGFuZGxlIjoyMjU0MzQ4NzI0MDMyOTE5Nzc2LCJyZW1vdGVhdXRoIjoiIiwic2NvcGUiOlsicmV4cm90aC1kZXZpY2UuYWxsLnJ3eCJdfQ.QRzUwVd8sGPn0-oa0qheO_C93aOCC7WwqDlcD3-3Gqajd4YWudPCTEQaTvyb5aF25tBRTSwjY-9lVzusJaFqqDQZ8UCri9IH8FsNcORuqjg6aIB7PVqE5ESmzBqPweGT";


// try to use a PUT command
fbWebClientPUT(
	xExecute			:= , 
	udiTimeOut			:= 1000000, 
	sURL				:= 'https://127.0.0.1/automation/api/v2/nodes/plc/app/Application/sym/GVL_Datalayer/dwTestDWORD', 
	eRequestType		:= WEB_CLIENT.REQUEST_TYPE.PUT,
	eContentType		:= WEB_CLIENT.CONTENT_TYPE.APPLICATION_JSON,
	pwsAdditionalHeader	:= ADR(wsBEARERToken), 
	pwsPostValue		:= ADR(wsPOSTCommand), 
	xCloseConnection	:= , 
	hCert				:= , 
	itfTLSContext		:= , 
	itfAsyncProperty	:= , 
	xDone				=> , 
	xBusy				=> , 
	xError				=> , 
	eError				=> , 
	httpResult			=> );

 

To use this functionblocks you need the additional CODESYS library (IIoT libraries) and a license. See this link.

Note: In demo mode, the software runs for 30 minutes without a license. After that, a manual restart is required.

View original
2 replies