简体   繁体   中英

Sending Complete JSON Data from the CSV file in one request in JMeter

I'm trying to send a JSON request in JMeter

{"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

{"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.

Given that your CSV file looks like:

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:

  1. Add a Beanshell Preprocessor as a child of the HTTP Request sampler, which should send that JSON data
  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
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM