Sending an http post request - help with groovy


(Takis) #1

hello all, I have been successfully sending requests to my device, but i need to make my coding a little more versatile. this is what i have so far:

def post_to_dev(def dev_link, def dev_type, def dev_action) 
{
log.debug "sending request to device: ${dev_link} -> ${dev_type}:${dev_action}"
def params = [
    uri: "https://xxxxxx.com/${dev_link}",
    body: ["${dev_type}" : "$dev_action"]
]
try {
        httpPostJson(params)
    } 
    catch (e)
    {
        log.debug "something went wrong: $e"
    }
}

sending like this:

post_to_dev("xxxxxxxxxxxxxxxxxx" , "pin9" , "ON")

when i send pin9: ON it never goes as "ON"
my problem is at this line:

    body: ["${dev_type}" : "$dev_action"]

i need the “$dev_action” to br variable.
I have tried:
"$dev_action"
$dev_action
dev_action

nothing works. It only works when i replace specifically

body: ["${dev_type}" : "ON"]

or

body: ["${dev_type}" : "OFF"]

Could someone help?


(sidjohn1) #2

Have you tried changing body: ["${dev_type}" : “$dev_action”] to body: ["${dev_type}" : “${dev_action}”]
You may also want to add log.debug “${dev_action}” right above it to verify it has the value you expect.


(Takis) #3

thanks for the reply yes i tried it.
when i do this i receive this on the other side:

{"pin9":{"bytes":[79,78],"strings":["",""],"valueCount":1,"values":["ON"]}}

when i send a body: ["${dev_type}" : “ON”] i receive:

{"pin9":"ON"}

which is what i want.
Any other ideas?


(sidjohn1) #4

it sound like you are getting more data than you want from $dev_action. $dev_action.values, may work or you can ensure that $dev_action just has the value of “ON” by parsing your json better further up in your code.