0
votes

I have getuserdetails api where I need to send purchased item in array

API: api/getuserdetails/${id}
Method: post
body: {"uname":{
"purchaseitem": ["121","11","4","12345"]
}
} 

In jmeter setup looks like

>>TP_1
 >>CSVDataSet_ids
 >>CSVDataSet_purchaseitem
 >> ThreadGroup1
   >>HTTP Req
   >>JSR223 PreProcessor
 >>View result tree

purchaseitem csv has values like

1231
12121
312232
13
1
42435
133

I want to pass "purchaseitem" array values fetched from CSV in comma separated manner as shown in body

I've tried something like this in JSR223 PreProcessor

def list = ${purchaseitem}

for (i=0;i<=list.size()-1;i=i+10)
{
def mylist = list.subList(i,i+10);
props.put ("mylist"+i,mylist)
}

Can someone please help? Or is there any function to solve this simple problem?

1

1 Answers

2
votes

You're doing something weird

  1. You won't be able to use CSV Data Set Config because it reads the next value with next virtual user/iteration
  2. Your code doesn't make sense at all

If you need to send all the values from the CSV file at once you should be rather using JsonBuilder class, example code which generates JSON and prints it to jmeter.log file:

def payload = [:]
def purchasedtem = [:]
purchasedtem.put('purchaseitem', new File('/path/to/your/purchase.csv').readLines().collect())
payload.put('uname', purchasedtem)

log.info(new groovy.json.JsonBuilder(payload).toPrettyString())

More information: