[英]Sending Complete JSON Data from the CSV file in one request in JMeter
I'm trying to send a JSON request in JMeter 我正在尝试在JMeter中发送JSON请求
{"responseProtocol":"PROTOCOL_JSON","requestProtocol":"PROTOCOL_JSON","codes":"164","122","123","161","149","158"]}
Instead of hardcoding the values i tried saving them in a CSV file and looping through them.I tried using CSV data set config to loop,but the request is being sent this way 我没有对值进行硬编码,而是尝试将它们保存在CSV文件中并遍历它们。我尝试使用CSV数据集config进行循环,但是请求是以这种方式发送的
{"responseProtocol":"PROTOCOL_JSON","requestProtocol":"PROTOCOL_JSON", "codes":"164"}
{"responseProtocol":"PROTOCOL_JSON","requestProtocol":"PROTOCOL_JSON", "codes":"122"}
{"responseProtocol":"PROTOCOL_JSON","requestProtocol":"PROTOCOL_JSON", "codes":"123"}
It is sending one code for each request and sending multiple request while looping through the end of file. 它为每个请求发送一个代码,并在遍历文件末尾的同时发送多个请求。
Is there a way to send multiple codes in a single request. 有没有一种方法可以在单个请求中发送多个代码。
Currently JMeter doesn't provide a relevant test element or even a combination of test elements to implement your scenario so you'll have to bypass this JMeter limitation by some scripting. 当前,JMeter不提供相关的测试元素,甚至不提供测试元素的组合来实现您的方案,因此您必须通过一些脚本来绕过JMeter的限制。
Given that your CSV file looks like: 假设您的CSV文件如下所示:
164
122
123
161
149
158
And you're using HTTP Request sampler to send JSON via POST method, you can do it as follows: 而且您正在使用HTTP Request采样器通过POST方法发送JSON,您可以按照以下步骤进行操作:
Put the following code into the PreProcessor's "Script" area: 将以下代码放入预处理器的“脚本”区域:
StringBuilder requestBody = new StringBuilder(); requestBody.append("{\\"responseProtocol\\":\\"PROTOCOL_JSON\\",\\"requestProtocol\\":\\"PROTOCOL_JSON\\",\\"codes\\":["); BufferedReader reader = new BufferedReader(new FileReader(new File("/path/to/your/csv/file.csv"))); String line; while ((line = reader.readLine()) != null) { requestBody.append("\\"").append(line).append("\\"").append(","); } reader.close(); requestBody.append("]}"); sampler.setPostBodyRaw(true); sampler.addNonEncodedArgument("",requestBody.toString(),"");
/path/to/your/csv/file.csv
with the real path to your CSV file /path/to/your/csv/file.csv
替换为CSV文件的真实路径 That should be it. 应该是这样。 Run test with 1 thread and look into View Results Tree listener to ensure that everything is fine.
用1个线程运行test,并查看View Results Tree侦听器,以确保一切正常。
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter and a kind of cookbook with some popular Beanshell script recipes. 请参阅“ 如何使用BeanShell:JMeter的最喜欢的内置组件指南”,以获取有关JMeter中Beanshell脚本的更多信息以及一种包含一些流行Beanshell脚本食谱的食谱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.