Http POST

Hi,

I’m trying to build a simple integration with link-tap. It has an open API as an example I can control the device through the following post.

How would I go about executing this on a switch on? I looked at the custom http momentary switch device, although POST is an available option, how would I configure the parameters?

Many Thanks,

D

curl -d "username=YourUsername&apiKey=YourApiKey&gatewayId=YourGatewayId&taplinkerId=YourTaplinkerId&action=true&duration=20&eco=true&ecoOn=1&ecoOff=2" -X POST https://www.link-tap.com/api/activateInstantMode

Try @ogiewon's Virtual Http device.
Hubitat/Drivers/http-momentary-switch.src at master · ogiewon/Hubitat · GitHub

Thanks, I’ve got this driver, but for a http put, I’m not sure how to pass the parameters ...

Ahh. I'm not the greatest at figuring that stuff out myself but I brute force it until it bends to my will.

I use the Postman application to figure out the correct format and then copy the full link into whatever custom driver or app I need it for.

2 Likes

https://www.link-tap.com/api/activateInstantMode?username=YourUsername&apiKey=YourApiKey&gatewayId=YourGatewayId&taplinkerId=YourTaplinkerId&action=true&duration=20&eco=true&ecoOn=1&ecoOff=2

Easy, put a ? after the URL and then just add your params separated by &'s. At least that's how every other url that I've ever used works.

I think thats for a get rather than a post..

But that should only matter to the system sending the request, right? The URL is the same, whether it's a get a or post. Throw it into RM and try to send it as POST and see. I'm pretty sure that the post part just affects the requesting system not waiting for a response.

1 Like

You don't use POST as GET, it doesn't work the same.

If anything you should probably use PUT instead of POST.

https://www.w3schools.com/tags/ref_httpmethods.asp

1 Like

@ogiewon using your http switch how would I go about using a post? I see the option in the drop down but what is the syntax for the parameters?

Thanks so much in advance,

D

I’m not an expert and thus do not know how to properly format a POST request using this driver. I probably ported this from someone’s ST DTH. IIRC, I created this driver to work with the Harmony-API NodeJS server before I wrote the webSockets integration.

Wish I could be more help. You should be able to find some other groovy examples that issue POST commands and properly format the Headers and Body. You can then modify this driver to suit your needs.

2 Likes

I don’t have a LinkTap so no way to test but you should be able to create a virtual device and then create a RM 3.0 rule that triggers based on the state change of this switch. Here is an example:

The JSON string body needs to contain the required parameters as outlined here:

4 Likes

No worries, thanks for taking the time to reply.

All the best

Thanks so much for this and taking the time to mock this up, appreciated - I’ll give this a go.

Is it possible to parse the output from the post using RM so I can read the status of the device ?

D

Managed to get this working using syntax...

username=user&apiKey=key&gatewayId=id&taplinkerId=id&action=true&duration=1

Just need to figure out how I can parse the response to deal with any errors...

From RM I don't believe that is possible. You would have to do that in the driver. If you look at the HTTP Get switch driver, you can see an example:

def on() {
    if (logEnable) log.debug "Sending on GET request to [${settings.onURI}]"

    try {
        httpGet(settings.onURI) { resp ->
            if (resp.success) {
                sendEvent(name: "switch", value: "on", isStateChange: true)
            }
            if (logEnable)
                if (resp.data) log.debug "${resp.data}"
			if (autooff)
				runIn(delay, off)
        }
    } catch (Exception e) {
        log.warn "Call to on failed: ${e.message}"
    }
}

the "catch exception" would be where the error occurred and the "resp.success" is it working. If the system is going to respond with some type of data, that would be in the resp.data field, which could then be parsed however you want.

1 Like

Sorry for a newbie question. I have a similar problem as described in this thread, but can't figure out how I can install this virtual device? I.e, where can I find the code? :slight_smile:

I want to try it with the AiLight REST-api

The above screenshot you posted is not from a device. It is actually from Rule Machine. That's why it is hard to find! :wink:

You can use a Virtual Switch device (simply add a virtual device and assign it the virtual switch driver, no new code required.) Then, in Rule Machine, create a Rule that is triggered by the Virtual Switch changing. Within the Actions of that rule, add a HTTP action for ON and and another for OFF.

Thank you! for the quick response. I'm getting closer :smile:
Question now is what kind of syntax I need in the 'Enter body for POST' box.

Tried some stuff but can't get it to work...

This is the curl command that works today:
curl -X POST http://192.168.1.2/api/light -H 'API-Key: hinotori' -H 'Content-Type: application/json' -d '{"brightness": 36, "state": "ON"}'

-H is header in curl (-H 'API-Key: hinotori'). -d is the data portion of the command and that is what you would put in the body for the post ('{"brightness": 36, "state": "ON"}'). However, AFAIK, you can't modify headers with RM so it probably wouldn't work anyhow.

The only way I can think of to get curl command to work is by either creating a custom app in Groovy (which is actually really simple to do) OR use something like NodeRed or NodeJS to craft and send the command using the event socket output from Hubitat.

Ok. Thank you for the support! I think a third option is MQTT. I will look in to it :smiley:

1 Like

Download the Hubitat app