Json Format being sent to API as string

I'm at the end of my patience with this. This is was the first roadblock I had writing code on the smartthings platform, and I'm getting ready to start smashing my laptop to pieces, so I would greatly appreciate some assistance.

def refreshToken(){
refresh  = state.refreshToken
jsonMaps= '{"refresh_token":'refresh'}'

def postParams = [
    uri: "https://frdtyujk.requestcatcher.com/",
	    requestContentType: 'application/json',
	    contentType: 'application/json',
    body: ['user': ['refresh_token': refresh]]
        ]
asynchttpPost('token', postParams)

}

I am trying to get the above's body to look exactly like what is below when it is received by the api.

{
"user": {
"refresh_token": "someToken"
}
}

I am attempting to reference a state token. The only way I've been able to get the proper token received by the API is by mutilating the json. Nearly everything I try results in a failure to save the driver, a failure to make the call, or a 404. I do not know coding, I know only what I"ve been able to discern. I have played with quotations, brackets, single quote and parenthesis' and the placement of these things until I was ready to punch my laptop's monitor. Why is this is so freaking difficult? Yes, I am aware there is documentation. The documentation as far as I am aware does not explain how to get groovy to understand it is not the most important thing in the world

Here's how I do it (for a JSON payload):

body : new groovy.json.JsonOutput().toJson( [ grant_type: "password", client_id: clientID, client_secret: clientSecret, username: username, password: password ])

My question with this, and thank you by the way for taking the time to offer a solution, is will groovy get bent out of shape that there is an array within the json I’m trying to send? Adding in that second curly bracket or curly bracket equivalent is what has been causing the data to be sent as a string that says “state.refreshToken” rather than the actual token.

With your example there is a straightforward pair for each piece of data. The issue with my snippet comes into play after “user”. The second curly bracket which contains my refresh token is throwing all kinds of errors.

If I’m following, and if your method works with an array, I imagine I would add another bracket in place of a curly bracket.

I’ll give that a shot though when I’m about to look at my laptop without hard feelings.

Yes, I would expect that to work.

'JsonOutput().toJson()` just converts a groovy map into a properly formatted and escaped JSON string.

So, adding that second bracket as you described would declare the map member as a map which could then be re-formatted into the corresponding JSON object.

This still resulted in errors. I guess after looking at the request catcher history, the issue is that the refresh token is not being passed as anything other than the string used to reference it. Or "null".

A few examples of how it shows up in the request catcher is

"user":{"refresh_token":"${refreshToken}"}}

{"user":{"refresh_token":null}}
{"user":{"refresh_token":"refreshToken"}}
{"user":{}}

Try something like:

def refreshToken() {

    log.debug("state.refreshToken: ${state.refreshToken}")

    Map postParams = [ uri: "https://frdtyujk.requestcatcher.com/", requestContentType: "application/json", contentType: "application/json", body: new groovy.json.JsonOutput().toJson( [ user: [ refresh_token: state.refreshToken ] ] ) ]

    log.debug("postParams: ${postParams}")

    asynchttpPost("token", postParams)

}

def token(response) {

    log.debug("response: ${response}")

}
2 Likes

Ok, so this did work. I believe @tomw's solution should have worked correctly as well. In my frustration I failed to realize the refresh token I was trying to refresh had been null for quite some time, as the command I am working on was setting the refresh token with each call.

So for every failed refresh call, the refresh token was set to the response. Nothing.

I am an idiot.

Heh, it happens, and anybody who actually spends the time like you are to try to figure something out has those moments. Glad that you're on to the next challenge.

2 Likes

Download the Hubitat app