httpPost closure is eating my return value

I have these two function implementations that wrap httpGet(Map params, Closure closure) and httpPost(Map params, Closure closure).

In both cases, the debug logs of the responses contain the expected data. Returning that data in the httpGet case behaves correctly, but with httpPost it is returned as null. The alternate flavor does not have this issue: httpPost(String uri, String body, Closure closure)

I can work around this by assigning the response to some other global variable and passing it back that way, but I'd appreciate any ideas on what is causing this to not work directly. It's not an issue with the specific response data- returning anything from inside the closure fails, even plain old data.

   def httpGetExec(params, throwToCaller = false)
    {
    logDebug("httpGetExec(${params})")
    
    try
    {
        httpGet(params)
        { resp ->
            if (resp.data)
            {
                logDebug("resp.data = ${resp.data}")
                return resp.data
            }
        }
    }
    catch (Exception e)
    {
        logDebug("httpGetExec() failed: ${e.message}")
        if(throwToCaller)
        {
            throw(e)
        }
    }

}

def httpPostExec(params, throwToCaller = false)
{
    logDebug("httpPostExec(${params})")
    
    try
    {
        httpPost(params)
        { resp ->
            if (resp.data)
            {
                logDebug("resp.data = ${resp.data}")
                return resp.data
            }
        }
    }
    catch (Exception e)
    {
        logDebug("httpPostExec() failed: ${e.message}")
        if(throwToCaller)
        {
            throw(e)
        }
    }
}

@dman2306, any chance that you have seen this? Or can you offer a pointer on what is wrong with my usage?

It’s the way closures work in HE.

def result
httpPost(params)
        { resp ->
            if (resp.data)
            {
                logDebug("resp.data = ${resp.data}")
                result = resp.data
            }
        }
return result
1 Like

Cool, I'll give that a shot.

Just so I can learn, can you help me understand why it is different for the httpGet and httpPost wrappers that I made? I also have another driver that uses the other flavor of httpPost and it works. I assume it's something subtle about the usage here that I'm not seeing.

To my knowledge there is no difference and you must do it for httpGet as well. I’m not sure why that seems to work for you the way your code is written. It doesn’t for me

1 Like