httpPost - nested body

I am trying to generate a httpPost in my code using mapped parameters to send a command to a remote server similar to the code below:

def params2 = [
uri: "https:pathtoserver",
headers: [ "Content-type" : "application/json; charset=UTF-8" ],
headers: [ 'User-token' : "Hidden" ],
headers: [ "Application-Id" : "Hidden" ],
body: [CANT GET THIS CORRECTLY FORMATTED]
]
httpPostJson(params2,response);

Reading the documentation I understand that the body needs to be a map but the server requires the following JSON to correctly process the request:
{
"attrs": {
"heat_power": 0
}
}

Can the httpPost function handle a nested body? I have tried many many different way to get the above JSON into a map version that works but I just keep getting the following error:

groovyx.net.http.HttpResponseException: status code: 400, reason phrase: Bad Request on line 24 (method on)

I am sure this should be simple to-do but I am pulling my hair out and I am now questioning whether the built in function will process a nested body request

Many Thanks

Try this:

    cLine = '{'+"\"attrs\":[{\"heat_power\":\"0\"}]"+'}'
    try{
        params = [
					uri: "$pathToServer",
                    headers: [
                        Accept: "application/json",
                        "Content-Type":"application/json",
                        "User-token": "$token",
                       "Application-Id":"$appId",
                        textParser:false
                        ],
                    body:"$cLine"
				]    

        httpPost(params){ resp ->
            if(debugEnabled) "$resp.data"
        }
    } catch (e){
        log.error "Error on send: ${e} ${e.getResponse().data}"
    }
}
1 Like

Try this, to save your sanity with manual string entry (in case it gets more complicated than this simple case):

Map output = [attrs: [heat_power: 0]]
String body = new groovy.json.JsonOutput().toJson(output)
1 Like

Couldn't find my example with that code, know I have it because that's my usual method....

1 Like

I know the feeling -- I can tell at a glance whether my various HE integrations were developed "before" or "after" I learned about JsonOutput. :wink:

1 Like

Thanks @thebearmay but this doesn't seem to be working. If a debug the created string I get the following:

{"attrs":[{"heat_power":"0"}]}

modifying the code slightly I managed to get the following string:

{"attrs":{"heat_power":0}}

However I am assuming this is because although I want the body request to the server to be the exac string that we have created the function needs that passed as a mapped parameter and therefore the httpPost request is not correct.

Please forgive me if my understanding is not correct as I am still trying to get my head around hubitat syntax

Both are valid JSON (https://jsonlint.com/) , my format was taken from some code that had multiple second level values which is why it had the [ ].

Just try httpPost(), like this:

    Map output = [attrs: [heat_power: 0]]
    String body = new groovy.json.JsonOutput().toJson(output)
    
    def params2 =
        [
            uri: "https:pathtoserver",
            headers:
            [
                'User-token': "Hidden",
                'Application-Id': "Hidden"
            ],
            contentType: 'application/json',
            requestContentType: 'application/json',
            body: body
        ]
    
    httpPost(params2)
    { resp ->
        if (resp)
        {
            log.debug "resp.data = ${resp?.data}"
        }
    }

Thanks @tomw. This worked perfectly. I just need to spend a little time to understand where I was going wrong

Thanks @thebearmay . I suspect your responses woudl work as well and its my understanding that is a little wobbly.

Appreciate you guys taking the time to point me in the right direction

2 Likes

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