Search code examples
javajmeterbeanshell

Jmeter: Not able to use bean processor variable value in HTTP Request Body


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

Solution

  • 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.