Inconsistent Behavior with httpPost

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.

Some closure on this. Given this API is undocumented and not overly expressive, it's difficult to tell what is an issue with Groovy vs the API.

I've started taking the approach of avoiding using any translation in the httpPost method and just feeding straight JSON with the correct headers.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.