Hey all. Just started working on my first Hubitat App over the weekend. New to Groovy so apologies if any of this is innacurate:
In short I'm attempting to make REST API calls using JSON. I've found you can pass httpPost
a map as the body with the correct options set. The following DOES work when authenticating to get an access token:
def params = [
'uri' : wyzeAuthBaseUrl(),
'headers' : wyzeRequestHeaders(),
'requestContentType' : "application/json; charset=utf-8",
'path' : "/user/login",
'body' : [
'email': settings.username,
'password': settings.hashedPassword
]
]
try {
httpPost(params) { response ->
...do stuff
}
} catch (Exception e) {
logDebug("Login Failed with Exception: ${e}")
return false
}
However, the following DOES NOT work when sending an API call:
def params = [
'uri' : wyzeApiBaseUrl(),
'headers' : wyzeRequestHeaders(),
'requestContentType' : "application/json; charset=utf-8",
'path' : path,
'body' : body
]
try {
httpPost(params) { response -> logDebug(response.data) }
} catch (Exception e) {
logDebug("API Call to ${params.uri}${params.path} failed with Exception: ${e}")
return false
}
where body
is a Map similar to the previous call.
If I instead convert body
to JSON myself using (new JsonBuilder(body)).toString()
and send that, it works.
Any ideas on what I'm doing wrong? Seems I should just be able to use the map and avoid using JsonBuilder.