简体   繁体   English

在JMeter的一个请求中从CSV文件发送完整的JSON数据

[英]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,您可以按照以下步骤进行操作:

  1. Add a Beanshell Preprocessor as a child of the HTTP Request sampler, which should send that JSON data 添加一个Beanshell预处理器 作为 HTTP请求采样器的子代 ,该采样器应发送该JSON数据
  2. 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(),""); 
  3. Replace /path/to/your/csv/file.csv with the real path to your CSV file /path/to/your/csv/file.csv替换为CSV文件的真实路径
  4. Populate sampler configuration like host, port, path. 填充采样器配置,例如主机,端口,路径。 Do not put anything into "Parameters" or "Body Data" 不要在“参数”或“身体数据”中添加任何内容

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.

相关问题 JMeter - 保存对 CSV 文件的所有请求的完整 JSON 响应,用于测试数据准备 - JMeter - Save complete JSON response of all the request to CSV file for test data preparation 从 Jmeter 中的 csv 获取完整的 json 正文 - Get complete json body from csv in Jmeter 如何在 Z8F0466AE1A66FDAF3CED537A2 中的单个 HTTP 请求中从 CSV 文件发送多个 JSON 数据 - How to send multiple JSON data from CSV file in single HTTP request in JMETER tool 使用JMETER CSV日期设置配置时如何在请求正文中将CSV文件中的数据转换为JSON - How to have the data in the CSV file converted to JSON in the request body when using JMETER CSV Date set config 使用 JMeter 从 CSV 文件构建 JSON 批次 - Build JSON Batches from CSV file with JMeter Json的Jmeter CSV文件 - Jmeter CSV file for Json 对于jmeter post请求,如何从csv文件生成输入json? - For jmeter post request, how can I generate input json from csv file? 从csv创建jmeter groovy动态json请求 - jmeter groovy dynamic json request creation from csv Jmeter:如何使用 csv 文件中的字段名称构造 JSON 路径,以将 API 响应中的数据与 DB 中的数据进行比较 - Jmeter: how to construct JSON Path using field names from csv file to compare data from API response with data from DB 如何在JMeter中提取完整的JSON响应数据? - How to extract complete JSON response data in JMeter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM