Search code examples
groovyjmeterload-testingjmeter-5.0jsr223

Vars.get returns a null value


def signValue = '${signature_value}.${timestamp}.${signature_value}'
def token_secret = '${APP_CLIENT_SECRET}'
log.info("token is " + signValue)
def signingKey = new javax.crypto.spec.SecretKeySpec(signValue.getBytes(),"HmacSHA256");
def mac = javax.crypto.Mac.getInstance("HmacSHA256")
mac.init(signingKey);
def hmac = mac.doFinal(token_secret.getBytes());
def result = hmac.encodeBase64().toString()
---- I want to use the above "result" variable into a Http sampler request body------
---- I tried many possible ways but I end up is getting value as null or some error--
//${__groovy(vars.get("result"))}
//vars.put("signature", vars.get(result))

I've been trying to extract the value of the variable "result" and use it in HTTP sampler results. But I ended up getting a null value or some other error. Anyone could help me to sort out this problem.

Thanks!


Solution

  • Change this line:

    vars.put("signature", vars.get(result))
    

    to this:

    vars.put("signature", result)
    

    Also don't inline JMeter variables into Groovy scripts like this:

    def token_secret = '${APP_CLIENT_SECRET}'
    

    use vars shorthand instead:

    def token_secret = vars.get('APP_CLIENT_SECRET')
    

    Because:

    • It conflicts with Groovy GStrings

    • For best performance it's recommended to cache compiled scripts (enabled by default)

      enter image description here

      and in this mode JMeter resolves only first value and caches it which means you will get the same value for every iteration

    More information: