REST API communication example
-
Hi,
I have a question related to example \0302_IoT_Integrations\0302-03_REST_API_Python_Bash_etc
and Matlab REST API communication.
If I run this example with the default admin credentials
(admin and empty password), using
options.Password = ''; in getdata.m ,
I get the following error:The content reader issued the error message: "Unable to parse the JSON data while reading the content downloaded from URL 'http://127.0.0.1:8008/login?u=api/login'
I think it reads the html content of the initial webpage and it does not overcome the issue related to the empty password.I can of course set some non-empty password, or I can implement some workaround such as setting
auth.enabled=0 in rexcore.cfg.But still I'd like to ask if there's a way how to run this example
with blank password without modifying rexcore.cfg.
Thanks, Stepan -
@stepan-ozana It seems like it behaves the same even if I set some password for admin user. Can you please double check if this example works for you?
-
@stepan-ozana
Dear Štěpán,
I can confirm that the Matlab script behaves the same way for me as it does for you. Thanks for reporting the bug. Can you please test the following modified script? This modification works for me:clc; clear all; serviceUrl = 'http://192.168.100.2:8008'; username = 'admin'; password = ''; % Data to be written value_double = 17.89; value_long = 1234; value_bool = 1; value_string = "External string"; % URLs of the data points url_double = "/api/tasks/rest_api_task/CNR_IN:ycn"; url_long = "/api/tasks/rest_api_task/CNI_IN:icn"; url_bool = "/api/tasks/rest_api_task/CNB_IN:YCN"; url_string = "/api/tasks/rest_api_task/CNS_IN:scv"; % Basic authentication header credentials = base64encode([username ':' password]); authHeader = ['Basic ' credentials]; % Writing data type double data_double = struct('v', value_double); writeData(serviceUrl, url_double, data_double, authHeader); % Writing data type long data_long = struct('v', value_long); writeData(serviceUrl, url_long, data_long, authHeader); % Writing data type Boolean data_bool = struct('v', value_bool); writeData(serviceUrl, url_bool, data_bool, authHeader); % Writing data type string data_string = struct('v', value_string); writeData(serviceUrl, url_string, data_string, authHeader); % Function to encode base64 function encoded = base64encode(str) encoder = java.util.Base64.getEncoder(); encoded = char(encoder.encodeToString(uint8(str))); end % Function to write data to the server function writeData(serviceUrl, url, data, authHeader) options = weboptions('RequestMethod', 'post', ... 'ContentType', 'json', ... 'MediaType', 'application/json', ... 'HeaderFields', {'Authorization', authHeader}); try response = webwrite(strcat(serviceUrl, url), data, options); disp(['Successfully wrote data to ', url]); catch ME fprintf('Error writing data to %s:\n%s\n', url, ME.message); end end
Cheers,
Jan -
@Jan-Reitinger Thanks for your assistance. I confirm that this modified script works fine.
-
@stepan-ozana
Thank you for confirmation. I will update the example. Unfortunately, quite large changes occur across Matlab versions, and there are situations where a script from older versions stops working in newer ones.Cheers,
Jan