REXYGEN Community Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Login

    REST API communication example

    Communication (RS232, RS485, I2C, SPI, UDP, TCP, ...)
    2
    5
    230
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      stepan.ozana
      last edited by

      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

      S 1 Reply Last reply Reply Quote 0
      • S
        stepan.ozana @stepan.ozana
        last edited by

        @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?

        J 1 Reply Last reply Reply Quote 0
        • J
          Jan Reitinger @stepan.ozana
          last edited by Jan Reitinger

          @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

          S 1 Reply Last reply Reply Quote 0
          • S
            stepan.ozana @Jan Reitinger
            last edited by

            @Jan-Reitinger Thanks for your assistance. I confirm that this modified script works fine.

            J 1 Reply Last reply Reply Quote 0
            • J
              Jan Reitinger @stepan.ozana
              last edited by

              @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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post

              This is a community forum for REXYGEN users and fans. Detailed information can be found at REXYGEN homepage.

              There is also an outdated REXYGEN community forum.

              Powered by NodeBB.