CORS Problem at REST API

I’m trying to make a simple GET call to the OH2 REST API, but I get an error, which says that my Cross-Origin request is blocked, because of ‘Access-Control-Allow-Origin’ is missing. I’m doing the request from another domain.

I already found this topic and uncomment the org.eclipse.smarthome.cors:enable=true in the smarthome.cfg file.

My request looks like this:

$.ajax({
type: ‘GET’,
url: ‘http://127.0.0.1:8080/rest’,
success: function(data) { alert(‘GET completed’); },
error: function() { alert(‘Failed!’); }
});

What am I missing?

No idea. @Stratehm, can you help?

Just tried with the repository version of SmartHome and it works.

Here the test file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body >
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script type="text/javascript">
          $.ajax({
            type: 'GET',
            url: 'http://127.0.0.1:8080/rest',
            success: function(data) {
                  alert('GET completed');
                $(document.body).append(JSON.stringify(data));
            },
            error: function() { 
                  alert('Failed!');
            } 
          });
        </script>

</body>
</html>

The file is served from another domain to trigger a CORS request. Here is the result of the request:

{"version":"0.8.0.qualifier","links":[{"type":"uuid","url":"http://127.0.0.1:8080/rest/uuid"},{"type":"bindings","url":"http://127.0.0.1:8080/rest/bindings"},{"type":"discovery","url":"http://127.0.0.1:8080/rest/discovery"},{"type":"links","url":"http://127.0.0.1:8080/rest/links"},{"type":"setup","url":"http://127.0.0.1:8080/rest/setup"},{"type":"thing-types","url":"http://127.0.0.1:8080/rest/thing-types"},{"type":"items","url":"http://127.0.0.1:8080/rest/items"},{"type":"inbox","url":"http://127.0.0.1:8080/rest/inbox"},{"type":"things","url":"http://127.0.0.1:8080/rest/things"},{"type":"sitemaps","url":"http://127.0.0.1:8080/rest/sitemaps"},{"type":"iconsets","url":"http://127.0.0.1:8080/rest/iconsets"}]}

I’m trying to make a simple GET call to the OH2 REST API

Are you testing OpenHAB2 or Smarthome ? Could you tell us the version ?

1 Like

Thanks @Stratehm and @Kai for your answers, it’s still not working.

I pulled the newest version from the git and uncommented again. I use the openHAB2 Version with ESH and I’m running the openHAB_Runtime. Then I copied your get example and it still goes into the error part. Both chrome and firefox show this message:

XMLHttpRequest cannot load http://127.0.0.1:8080/rest. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘file://’ is therefore not allowed access.

Do I have to run a specific Plug-in in the Run Configurations? What else did you do to make it work?

I installed this extension for chrome and now it works. Is there another solution for my problem than installing such an extension to the browser? Because as answered here this is not the solution for this problem.

Furthermore, why can I open an url with the browser(similiar to a GET request), but not inside of my html document?

I found this website and tested it, but as you can see in the pictures it is still wrong. In addition to my other questions, which bindings do you use when you run your OH2?

I installed this extension for chrome and now it works. Is there another solution for my problem than installing such an extension to the browser? Because as answered here this is not the solution for this problem.

Indeed, it is not a “good” solution since it is not portable.

Furthermore, why can I open an url with the browser(similiar to a GET request), but not inside of my html document?

Because of the Same Origin Policy

which bindings do you use when you run your OH2?

I have only tested SmartHome, not OpenHAB 2. Is there some prerequisites or configuration to use the openHAB_with_ESH launcher ? When I try to launch it in eclipse I have a lots of missing constraints.

1 Like

This launch config can only be used if you used “openHAB 2 with ESH” as an installation option in the Eclipse Installer and thus you have the ESH bundles as sources in your workspace.
But for testing this, you do not need them in your workspace. Simply use the normal “openHAB Runtime” launch config and it all should start up.

1 Like

CORS works with OpenHAB 2.

Try to paste this line in the OpenHAB2 services.cfg configuration file instead of uncommenting the line in smarthome.cfg:
org.eclipse.smarthome.cors:enable=true

@Kai may confirm that OpenHAB2 runtime does not use the smarthome.cfg file.

1 Like

As far as I know, openHAB 2 does not have a smarthome.cfg file at all - so yes, I can confirm that this isn’t used. services.cfg is indeed the right place.

1 Like

I thought about something like that, but I didn’t really had a clue. Thank you guys, it’s working now!

1 Like

Worked when inserting a line “org.eclipse.smarthome.cors:enable=true” into services/runtime.cfg

3 Likes
  • Platform information:
    • OS: Linux
    • Java Runtime Environment: 1.8
    • openHAB version: 2.3
  • Issue of the topic: I have a frontend application developed in Angular 6 running on my local machine (http://localhost:4200). This frontend calls to OpenHab Runtime (2.3) that it’s running on another machine (http://localhost:3000). When the frontend call to server, it responds with a No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:4200’ is therefore not allowed access. The response had HTTP status code 401. I configure it with org.eclipse.smarthome.cors:enable=true in the services/runtime.cfg.

What’s going wrong?

Thanks

Angel,

I have the same problem. Did you get the issue solved?

Thanks,
Bert

Hello!

Does anyone solved this problem? I’m also trying to make my own app using Vue.js, but I’m also running into CORS errors.

I have the reverse proxy running on nginx as documented and tried adding headers to the config, but then it says that there are mulitple values set:

has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, *', but only one is allowed.