Search code examples
jmeterload-testingbeanshell

How to get Sampler Body data from beanshell Pre-Processor - JMeter


I have Http Sampler body as follows, {"Uname":"admin","Signature":"${Sign}","LoginTime":"${LogTime}","Action":"Do_Action"}

I have to get the value of "Action" from the above body, and that value nned to be sent to Pre-processor which will be useful to do further action.

Help me out of this...!!

Thanks!


Solution

  • I would recommend switching to JSR223 PreProcessor and Groovy language as:

    1. Groovy has built-in JSON support
    2. Groovy performs much better than Beanshell

    Example Groovy code to extract "Action" from the request body and store it into ${action} JMeter Variable will look like:

    def body = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
    vars.put('action', body.Action)
    

    See Groovy is the New Black article for more details.