[英]How to have the data in the CSV file converted to JSON in the request body when using JMETER CSV Date set config
I have a CSV dataset config pointing to a CSV file with the following data: 我有一个CSV数据集配置,指向具有以下数据的CSV文件:
Ids
87541
4551
15441
11117
.....
n
Instead of looping through the file and do multiple POST
requests for each value, I need to have a single POST
request and pass ALL the IDs the request body which should like this in the generated JSON
: 我不需要遍历文件并为每个值执行多个
POST
请求,而是需要有一个POST
请求,并将所有ID传递给请求主体,而在生成的JSON
应该这样:
{
"ids": [
84280,
2334,
235,
32554,
3663,
346,
344643,
....,
n
]
}
Put the following code into "Script" area 将以下代码放入“脚本”区域
def csvfile = new File('test.csv') def jsonBuilder = new groovy.json.JsonBuilder() jsonBuilder { ids csvfile.collect { it } } vars.put('requestBody', jsonBuilder.toPrettyString()) log.info(vars.get('requestBody'))
The above code will read test.csv
file in JMeter's "bin" folder and create an ids
JSON Array where each element will be a line from the given file and put the result into ${requestBody}
JMeter Variable 上面的代码将读取JMeter的“ bin”文件夹中的
test.csv
文件,并创建一个ids
JSON数组 ,其中每个元素都是给定文件中的一行,并将结果放入${requestBody}
JMeter Variable
Demo: 演示:
References: 参考文献:
In CSV Data Set Config define delimiter which not in file as ~
在CSV数据集配置中,将不在文件中的定界符定义为
~
Put in variable name ids
for example. 例如,输入变量名称
ids
。
and then use it in request as { "ids": [ ${ids} ] }
然后在请求中将其用作
{ "ids": [ ${ids} ] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.