LAN Device: hubaction POST w/JSON using REST call?

I can’t seem to figure out how to POST a JSON object using hubaction. I can’t attach anything to the “body”.
Using these docs: http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/building-lan-connected-device-types/building-the-device-type.html

Clearly there’s some kind of method for UPnP/SOAP since they have “action” and “body”… but when I try to do the same using a REST call nothing gets attached.

I have managed to hack something by adding my json object into the header, but that is less than ideal. Is there a way to do a rest call with https so that it is encrypted?

My code where I do this is around lines 228 of this: https://github.com/oehokie/SmartDSC/blob/master/devicetypes/DSCAlarmThing.groovy

@Ben @urman ?

1 Like

Check out my code for ObyThing Music - https://github.com/obycode/obything/blob/master/ObyThingMusic.groovy

Take a look at this function:

private sendCommand(command) {
    def path = "/post.html"

    def headers = [:] 
    headers.put("HOST", getHostAddress())
    headers.put("Content-Type", "application/x-www-form-urlencoded")

    def method = "POST"

    def result = new physicalgraph.device.HubAction(
        method: method,
        path: path,
        body: command,
        headers: headers
    )

    result
}

Its just called with a string, so if you have a json object, convert it to a string - object.toString()

Hmmm… I am almost positive I tried that before and it didn’t work… But I don’t think I had some of that header line. I will try it when I get home.

I definitely converted it to a string too.

@obycode:
Just got home and tried it. I think it was a combination of things. I ended up using:

def json = new JsonBuilder()
json.call("command":"${command}","password":"${settings.hostpassword}")

def headers = [:] 
headers.put("HOST", "$host:$port")
headers.put("Content-Type", "application/json")

log.debug "The Header is $headers"

def method = "POST"

try {
    def hubAction = new physicalgraph.device.HubAction(
        method: method,
        path: path,
        body: json,
        headers: headers,
    )
   
    log.debug hubAction
    hubAction
}
catch (Exception e) {
    log.debug "Hit Exception $e on $hubAction"
}

And between that and an error I had on my server side (bodyParser was deprecated in Express 4 and I didn’t “use” it to begin with… oops…) that fixed it. Thanks!

2 Likes