How to make a https call

Is there a way to make a https post directly from a Hubitat groovy app? I found the http call in the API documentation but it isn't clear if it supports https and if so, what is required for it to work. I have a secure web server set up that I want to call from one of my apps. Right now I'm using a stupid workaround to call a local http app running on a rPi that then makes a secure https call. Is it as simple as putting "https://blahblahblah.html" in the URL field?

Not clear which method you are using, but yes in general HTTPS should be supported and should be as simple as using HTTPS in your URL. But there’s a catch the site you connecting to must use a certificate signed by a trusted authority, otherwise you have to jump through hoops to ignore certificate errors (which as a security best practice I don’t recommend).

Assuming the site you are connecting to isn’t using a self-signed certificate or signed by a private/internal authority, you should be fine. The signing authority needs to be trusted by your hub, and I don’t think there’s a way to modify the trusted authorities on the hub. If you are using a self-signed certificate it’s better to switch to using a externally signed certificate. You can get one for free from https://letsencrypt.org/. These work just fine with Hubitat, as long as your DNS name used in your URL matches the certificate.

Like this maybe??

	def params = [
		uri: "https://hubitatpackagemanager.azurewebsites.net/graphql",
		contentType: "application/json",
		requestContentType: "application/json",
		body: [
			"operationName": null,
			"variables": [
				"searchQuery": pkgSearch
			]
		],
		ignoreSSLIssues: true
	]
	
	def result = null
	httpPost(params) { resp -> 
		result = resp.data
	}
4 Likes

Yes you can. I do it in my GCal Search app for gets and posts. See the code starting after line 255.

3 Likes

Thanks everybody - I tried this tonight and it works like a charm. I got rid of my stupid middle-man hack and now I'm happy with the direct update. I ended up using the "asynchttpPost" function and I'm very happy with the experience it gives the user

2 Likes

For completeness sake, here is the function I wrote that I call and works perfectly. I modeled this after the snippet @csteele shared and the source I had for HomeBridge.

void sendHttpPost(ip, port, Map body) {
    def contentType = "application/json"
    Map params = [
        uri: "${ip}:${port}",
        requestContentType: contentType,
        contentType: contentType,
        body: body,
        ignoreSSLIssues: true,
        timeout: 20
    ]
    asynchttpPost("asyncHttpCmdResp", params, [execDt: now()])
}
void asyncHttpCmdResp(response, data) {
    def dt = now() - data.execDt
    logger("Resp: ${response} | Process time: ${dt}", "debug")
}
3 Likes