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
Script 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());
}
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.