[SOLVED] HTTP GET Command to Control Device via REST API

I’m not sure you can do that with the HTTP binding. I also think you have to sent a POST and not a GET. You can try to solve you issue through a rule using the “sendHttpPostRequest” function:

var output = sendHttpPostRequest("http://ip-homepilot/devices/#DeviceId#", "application/json", '{"name": "GOTO_POS_CMD", "value": "' + positionItem.state + '"}'

Thank you for your answear.
It does not work.

sitemap:

sitemap home2 label="test ui" {
	Switch item=test label="test"
	}

rule:

rule "testcase"
when
	Item test received update ON
then
	sendHttpPostRequest("http://172.21.10.182//devices/1010013", "application/json", '{“name”: “GOTO_POS_CMD”, “value”: 50}')
end 

item:

Switch test

You have an extra / in the url (rigth after the ip-address) try removing it.

Thank you Anders!
I removed it, but it does not work.

Read through the web page you posted earlier, apparently it’s supposed to be a PUT-request, not a POST. Change to sentHttpPutRequest() and see if that works.

1 Like

Because your payload contains quote marks, you have to try a bit harder to get that passed to the http action. The rules interpreter is looking at those quotemarks, and treating them like it does any other quotemark. “Ooh, a string that starts here and stops there.”

Here’s an example

my new rule file. It does not work.

rule "testcase"

    when
    	Item test received update ON
    then
        var url = "http://172.21.10.182/devices/1010013"
        var contenttype = "application/json"
        var POSTrequest = '{“name”: “GOTO_POS_CMD”, “value”: 20}'

        var output = sendHttpPostRequest(url, contenttype, POSTrequest)
        
     
    end

Detail? You’ve looked in openhab.log?

The double quotemarks you’ve used here

var POSTrequest = '{“name”: “GOTO_POS_CMD”, “value”: 20}'

are not the same as these

var POSTrequest = '{"name": "GOTO_POS_CMD", "value": 20}'

Do you know which sort you really need ?

See also my answer above, PUT not POST.

2 Likes

@markymark

From the answers above, it should be:

rule "testcase"

when
	Item test received update ON
then
    var url = "http://172.21.10.182/devices/1010013"
    var contenttype = "application/json"
    var PUTrequest = '{"name": "GOTO_POS_CMD", "value": 20}'

    var output = sendHttpPutRequest(url, contenttype, PUTrequest)
    
 
end
2020-01-24 11:02:26.741 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home2.sitemap' has errors, therefore ignoring it: [2,2]: required (...)+ loop did not match anything at input 'switch'

2020-01-24 11:02:44.822 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'home2.items'
2020-01-24 11:02:53.767 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home2.sitemap' has errors, therefore ignoring it: [2,2]: required (...)+ loop did not match anything at input 'switch'

2020-01-24 11:02:58.508 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home2.sitemap' has errors, therefore ignoring it: [2,2]: required (...)+ loop did not match anything at input 'switch'

2020-01-24 11:04:19.930 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home.sitemap' has errors, therefore ignoring it: [44,1]: no viable alternative at input 'switch'

2020-01-24 11:04:50.326 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'home.sitemap'
2020-01-24 11:05:26.350 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'home.sitemap'
2020-01-24 11:05:33.630 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'home2.sitemap'

Okay, you’ve got two broken sitemaps. This is does not relate to your rule. Unless … what file are you editing your rule into?

testcase.rule

That’s one error then. Rules files need to named like xxx.rules, even if there is only one rule in it.

When you edit a rules file, you can check in openhab.log to see if the system loaded it or complains about it.

1 Like

Now there are the errors of the rules :slight_smile:

2020-01-24 13:45:15.957 [WARN ] [.core.internal.folder.FolderObserver] - Error while opening file during update: C:\openHAB\conf\rules\testcase.rules
2020-01-24 13:45:16.342 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'testcase.rules'
2020-01-24 13:45:16.342 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'testcase.rules' is either empty or cannot be parsed correctly!
2020-01-24 13:45:17.584 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'testcase.rules' has errors, therefore ignoring it: [1,6]: no viable alternative at input 'â'

Don´t know what openHAB means at the last line

It does not help when you only post the errors here.

Please post your home.sitemap, home2.sitemap and your testcase.rules here.

Thanks.

It means it doesn’t like the content of your rules file.
Given the funny character, you probably need to look into the character encoding your editor uses when it saves a file.

Encoding is ANSI

home2.sitemap

sitemap home2 label="test ui" {
Switch item=test label="test"
}

testcase.rules

rule “testcase”

when
	Item test received update ON
then
    var url = "http://172.21.10.182/devices/1010013"
    var contenttype = "application/json"
    var PUTrequest = '{"name": "GOTO_POS_CMD", "value": 20}'

    var output = sendHttpPutRequest(url, contenttype, PUTrequest)
    
 
end

Your next job then, is to find out what encoding it should be for openHAB to use it.

1 Like