Perform httpPost with Data values

So I am trying to add a new function to my app and I believe i have a need to perform a httpPost. I do extensive HTTPget actions in my app and manipulate the headers and body, but this call is confusing me.

I have a working example of what needs to be submitted in Curl. A good example of it is this

curl --location 'https://xx.xxx.com' \
--header 'Content-Type: application/json' \
--data '{
"email": "xxxxxxxx",
"password": "xxxxxx"
}'

What is throwing me is this --data information. Is that something we can even attach in the HTTP post commands we can do in Hubitat. If so, do we have any examples of it anywhere. Everything i have found indicates it doesn't get put in the body though that could certainly be wrong.

The purpose of this call is so that I can parse the return and grab a token that is part of the response.

Here is an example from my Logitech Harmony Hub Parent driver.

        httpPost(uri: "http://${ip}:8088",
                 path: '/',
                 contentType: 'application/json',
                 requestContentType: 'application/json',
                 headers: ['Origin': 'http://sl.dhg.myharmony.com'],
                 body: '{"id": 1, "cmd": "setup.account?getProvisionInfo", "params": {}}'
                ) { response ->
             //activeRemoteId is the new property name instead of just remoteId.
             if (logEnable) log.debug "hub remote id: ${response.data.data.activeRemoteId}"
             state.remoteId = response.data.data.activeRemoteId
         }
1 Like

So does that mean the data values just go in the body section?

I believe so. Give it a try and let us know if it works for your application.

I got it to work and am now able to obtain the information I was looking for thanks. I did have to substring my data together for the "body" part of the values. I didn't expect that to work from what i was reading.

Thanks

1 Like

Awesome! Have fun!

Not sure exactly what you mean with this, but in case it's helpful to know, I believe Groovy collections (like a Map) will be automatically converted to JSON if that is what you're manually stringing together here (though it may depend on contenttype, and I've only done it with PUT but would expect POST to work similarly). If nothing else, there are also methods to convert these to JSON strings yourself.

But, again, not sure if that's what you mean--or if this would indeed be easier for you either way.

1 Like

It is probably just a sign of my lack of programming experience. :sweat_smile: I had two input values i needed to add to a string that made up the body data. When i tried to do that intially it wasn't pulling the value but just showing input name. String it all together was easy enough to make sure it was formatted properly.

Now i got a new interesting problem for you. It seems part of my message is being returned with the decimal value for the ASCII character instead of the string of char values. Any ideas why this would happen on a C8 and not a C7

An example of this would be my C7 returns this data:
token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImFjY291bnQiOiJ7XCJhY2NvdW50SWRcIjpcIjE

My C8 using the exact same code is returning this:
token is [101, 121, 74, 104, 98, 71, 99, 105, 79, 105, 74, 73, 85, 122, 73, 49, 78, 105, 73, 115, 73, 110, 82, 53, 99, 67, 73, 54, 73, 107, 112, 88, 86, 67, 74, 57, 46, 101, 121, 74, 107, 89, 88, 82, 104, 73, 106, 112, 55, 73, 109, 70, 106, 89, 50, 57, 49, 98, 110, 81, 105, 79, 105, 74, 55, 88, 67, 74, 104, 89, 50, 78, 118, 100, 87, 53, 48, 83, 87, 82, 99, 73, 106, 112, 99,

Any Ideas?

This would be a question for @gopher.ny from the Hubitat team. I believe the C8 hub may be running a newer JVM platform, which is just a hypothesis for why one might see differences between the two platforms.

Ok this is getting even stranger.

In the screenshot above from my C8 it shows the format of the call. What is really interesting about the returned data is effectively backwards, but it does show the token properly.

Maybe the problem is in the default HttpResponseDecorator.

Hopefully @gopher.ny has an idea as to what is going on.

Here is my Dev C7 Output to the logs

See how the response data starts with the Satus of 200 and then message saying "Login Sucessful"

Here is the snippit of code that generated the above data

            logger("sceneExtract(): bodyparm to be passed:  ${bodyParm}", 'error')
            def params = [
                uri   : 'https://community-api.govee.com',
                path  : '/os/v1/login',
                headers: ['Content-Type': 'application/json'],
                body: bodyParm
            ]
            logger("sceneExtract2(): parms to be passed:  ${params}", 'error')
            try {
                httpPost(params) { resp ->
                    status = resp.data.status.value
                    msg = resp.data.message.value
                    state.goveeHomeToken = resp.data.data.token.value.toString()
                    state.goveeHomeExpiry = resp.data.data.expiredAt.value
                    logger("sceneExtract2(): Substring data ${resp.data}", 'error')
                    logger("sceneExtract2(): Substring status is ${status}: Message ${msg}", 'error')
                    logger("sceneExtract2(): token is ${state.goveeHomeToken} and expires at ${state.goveeHomeExpiry}", 'error')
                    }
                } catch (groovyx.net.http.HttpResponseException e) {
                    logger("deviceSelect() Error: e.statusCode ${e.statusCode}", 'error')
                    logger("deviceSelect() ${e}", 'error')

                return 'unknown'
            }        
 //       }
    }

I found the cause of the issue. It seems that by adding .value to the end of those parsing strings cause the numeric data to appear. As soon as I removed that it was back to characters.

That doesn't address the data being in a weird order, but that doesn't matter to much with the way the parsing works with the levels.