Basic httpGet not working

I have just started toying around with creating an app for my hub. I just want to periodically make a web request to my server, however it seems that this isn’t working at all because no action is taken (the server isn’t receiving anything). My function is just this (with the server IP hidden):

def initialize() {
	// TODO: subscribe to attributes, devices, locations, etc.
    try {
	while (1 == 1) {
        httpGet('**.**.**.**:****/path')
        
        pause(30000)
    }
    } catch (e) {
        log.error "something went wrong: $e"
    }
}

Any idea what is going on? Do I need special permissions to run this?

Are you using a local IP (i.e. 192.168…")?
httpGet(‘192.168.1.233/path’) - wont work
httpGet(’‘myhome.no-ip.org/path’) - will work.

For local ip try sendhubcommand (or something like that)

No, it’s a public IP, 54.229.etc…

This may work once, but the initialize() call will quickly fail; your function callbacks in a smartapp (or device handler) have a limited time to perform operations (30 seconds?); your idea of having an infinite loop with requests every 30 seconds will not work.
Your best option is probably to schedule a time based event happening every minute or so (timed events are not too reliable, but timed events with intervals < 60 seconds are totally unreliable).

1 Like

Ah I see. Well I reworked my code to this, and it still isn’t calling every 5 minutes or so, not even once.

def handlerMethod() {
    try {
    	httpGet('*.*.*.*:****/path')
        
    } catch (e) {
        log.error "something went wrong: $e"
    }
}

def initialize() {
    runEvery5Minutes(handlerMethod)
}

Never tried httpGet without specifying a closure; maybe you can try something like:
httpGet(url) {response -> log.trace response}

What do you get in the log? Maybe you can add a log.trace just before httpGet, to make sure your method is being called?

1 Like

Right, the response will be sent to the closure passed to httpGet().

http://docs.smartthings.com/en/latest/smartapp-developers-guide/calling-web-services-in-smartapps.html#handling-the-response

Thank you very much! It is working now :slightly_smiling:

1 Like