FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important
information on the platform change.
04-04-2024 09:35 AM
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 => );
Solved! Go to Solution.
04-04-2024 12:29 PM
Moved to corresponding sub forum CODESYS - PLC Add-ons.
As I do not have access to this library I cannot do tests but in CODESYS please beware that there is a difference if a character or a string is declared with '' or "". Especially when using trings including such character like in your case.
It also seems you are missing the "content-type" : "application/JSON". Please check out "Using the REST API of ctrlX CORE".
04-05-2024 01:39 PM
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.