cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

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

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

LunatiX
New Contributor

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')

LunatiX_1-1712215218664.png

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

LunatiX_2-1712215334516.png

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:

LunatiX_3-1712215653080.png

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			=> );

 

 

 

2 REPLIES 2

CodeShepherd
Community Moderator
Community Moderator

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".

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.

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist