-1

I have a input CSV data file and have a 2 MB file which I need to send with each HTTP request to server. This file has 2 variables. I need to change those variables value from my input file. I'm using Bean shell pre processor. Below is the code snippet. I got the result in 'temp' variable not able to replicate it in HTTP Request Body. I've tried even ${temp} but it's not even working added variable in HTTP Body Script structure

HTTP Request and Bean shell structure :

import java.io.*;
//cardId,receiverid
try
{
    // reading file into buffer
    StringBuilder data = new StringBuilder();
    BufferedReader in = new BufferedReader(new FileReader("Sample.json"));

    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = in.read(buf)) != -1) {
    data.append(buf, 0, numRead);
    }
    in.close();

    // replacing stub with actual value
     System.out.println(vars.get("cardId") + " " +vars.get("receiverid") );

    String cardId = vars.get("cardId");
    String receiverid = vars.get("receiverid");
    String temp = data.toString().replaceAll("\\$\\{cardId\\}", vars.get("cardId"));
           temp = data.toString().replaceAll("\\$\\{receiverid\\}", receiverid);


    out.close();
}
catch (Exception ex) {
    IsSuccess = false;
    log.error(ex.getMessage());
    System.err.println(ex.getMessage());
}
catch (Throwable thex) {
    System.err.println(thex.getMessage());
}
M_Gandhi
  • 108
  • 2
  • 10

2 Answers2

0

First of all, I don't know if this is your problem, or you posted incomplete script, but I don't see you saving temp variable anywhere in the script. You need to have something like

vars.put("temp", temp);

to use it as ${temp} later in the script. Saving String temp is not going to create a variable automatically.

Second: you have a lot of problems with your script, so you really need to look at the log, nd make sure script runs properly. What I see is:

  1. I'm not sure how Sample.json looks, but based on what you are doing, it looks like you meant to replace \$\{cardId\} and \$\{receiverid\} in the data read from that file with the values of variables cardId and receiverid. But your code is incorrect, so only \$\{receiverid\} will get replaced. Correct it as follows:

    String cardId = vars.get("cardId");
    String receiverid = vars.get("receiverid");
    String temp = data.toString()
            .replaceAll("\\$\\{cardId\\}",cardId)
            .replaceAll("\\$\\{receiverid\\}", receiverid);
    
  2. And I don't see opening or declaration of out, so

    out.close();
    

    seems is not needed.

And one more thing: if you will have 1-2 users running such request 1-2 times, no problem, but if you need to create a load, this code will crumble: imagine 100 or more threads all trying to read the same 2MB file many times concurrently, and each of them putting it in the memory (potentially multiple copies of it during the replaceAll)? You will have more load on JMeter than server. There are better ways to store 2MB of static data, with only some vars repalced. But I won't go into details since this is not a topic of this question.

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
0

You are overscripting, you don't even need the Beanshell or whatever, everything can be done with JMeter Functions, to wit:

So if you put the following construction into the HTTP Request sampler "Body Data" tab:

${__eval(${__FileToString(Sample.json,,)})}

You will get the Sample.json file with all JMeter variables substituted with their values. See Here’s What to Do to Combine Multiple JMeter Variables article for more details.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133