performancegroovyjmeterjsr223

Receiving a data array via JSR223 PreProcessor jmeter


Task:

And here’s the question: what code should I add to it to solve my problem and pass the values I receive to the body of this request?


Solution

  • We cannot tell you "what code should you add to it to solve your problem" because we don't know how JMeter Variables originating from the Regular Expression Extractor look like and how do you need to transform them for the next request. In other words in order to get a comprehensive answer you need to ask a good question


    To give you an overall idea regarding what steps you need to take:

    If Regular Expression Extractor returns multiple matches like:

    VAR_1=foo
    VAR_2=bar
    VAR_3=baz
    VAR_matchNr=3
    

    you can create an "array" from them in the JSR223 PreProcessor like:

    def array = []
    
    1.upto(vars.get('VAR_matchNr') as int, { index ->
        array.add(vars.get('VAR_' + index))
    })
    
    vars.put('array', new groovy.json.JsonBuilder(array).toPrettyString())
    

    Demo:

    enter image description here

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?