Accessing item via rest API via myopenhab

Hi all,

to be able to render the location map (https://github.com/openhab/openhab/wiki/GoogleMap) also from remote in the native application I tried to access the location values via rest using https://user:password@my.openhab.org/rest … in the html; but it fails - I assume my.openHAB does not allow CORS (cross domain calls):

XMLHttpRequest cannot load https://my.openhab.org/rest/items/PatrikLocation/state/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://haus:8080' is therefore not allowed access. The response had HTTP status code 401.

Would it make sense to enable such requests, or is this too much of a security risk? Is there another way I can make the map work in the native apps (unfortunately there’s no location widget)?

with kind regards,
Patrik

That is correct

I’d have to think about it but my first inclination is this would be a pretty big security risk if not done really well. Jetty may not support it also, which would be technical challenge.

It seems that myopenhab doesn’t support CORS neither.

Using this code:

var request = $.ajax
        ({
            type       : "POST",
            url        :  "https://mail%40mail.be:password@home.myopenhab.org/rest/items/Z1_001_Heater",
            data       : "ON",
            contentType: 'text/plain',
            headers    : { 'Access-Control-Allow-Origin':'*' }
        });

I get following error:

No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

Do you know how to bypass this? When posting the request using Postman I have no problem at all but I must say I am not familiar with CORS yet.

Is there a solution to the issue?

I was also interessted into this use case and tested it. For me it worked fine to access openHAB via REST via myopenhab.

I tried two use cases:

  1. Get the state of an item
  2. Set the state of an item

Two ways of connection I tried:

My system setup:

  • openhabian on an Raspberry Pi connected via LAN
  • openhabian connected via openHAB cloud connector to the myopenhab cloud
  • I have one Thing with one Item (switch) set up via the Modbus binding
  • For testing REST I’m using the Mac App “Insomnia Core” (https://insomnia.rest/download/#mac)

My Insomnia Core setup for GET STATE:

My Insomnia Core setup for POST STATE:

This setup doesn’t work for me. Does anyone have any idea how I can access the REST api via myopenhab?

<html>
    <head>
        <script src="https://code.jquery.com/jquery-latest.min.js"></script>
    </head>
    <body>  
        <button id="ajax">ajax call</button>
        <script type="text/javascript">
            $('#ajax').click(function(){         
                $.ajax({ 
                     type: "GET",
                     username: "xxx@xxx.com", 
                     password: "xxx",
                     url: "https://myopenhab.org:443/rest/items/Test_Item/state",
                     success: function(data){        
                         console.log(data); 
                     },             
                 });
             });
        </script>
    </body>
</html>

I get the same error as described in the first post. Is there any other way to access items from “outside”?