Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSR223 PreProcessor.Passing data from the array to post request parameters

There are 2 requests.

In the first request through the Regular Expression Extractor I get data that looks like

VAR_1=foo
VAR_2=bar
VAR_3=base
VAR_N=NNN
VAR_matchNr=N

The data is stored in the variable VAR.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In the second request I added the JSR223 preprocessor. I added the following code to it:

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());

When the script runs, everything is processed correctly, the data from the VAR variable is converted into an array.

enter image description here

I have the following question:

I need to pass the received data in the parameters of a POST request. Right now I’m passing through the ${array} variable. But the entire array is transferred at once and the request is executed incorrectly. enter image description here

How can I make sure that each array value in the query parameters is written as a separate parameter? At the same time, I don’t know for sure how many parameters there will be, there can be from 0 to 20 enter image description here

>Solution :

You need to amend "your" code to create HTTP Request sampler parameters dynamically on the fly instead of saving the matches into the JMeter Variable.

Something like:

def arguments = sampler.getArguments()

1.upto(vars.get('VAR_matchNr') as int, { index ->
     arguments.addArgument(new org.apache.jmeter.protocol.http.util.HTTPArgument("list[]", vars.get('VAR_' + index)))
})

sampler.setArguments(arguments)

See Apache Groovy: What Is Groovy Used For? article for more information on Groovy scripting in JMeter

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading