Gatling || Need to pass image in request body (Base64 form)

Hi

I have a requirement to pass the image in Base64 format into my request, Below are the quires:-

  1. I have 500 images which I need to pass in each iteration, Is there any way to covert all the images into Base64 format dynamically during the test one by one
  2. In which Path/Folder i need to put my all images in Gatling, so that Gatling will read those image names and convert them in Base64 format and pass into my request body.

Please refer the request body:


{
  "requestPayload":
  {
    "image":"${image}"
  }

}

In Jmeter I was able to do this by JSR223 PreProcessor and CSV Data Set Config with the help of below code:

import org.apache.commons.codec.binary.Base64;
String var1 = vars.get(“FileNameFP”);

byte[] encodedPDF = org.apache.commons.io.FileUtils.readFileToByteArray(new File(var1));

String result = Base64.encodeBase64String(encodedPDF);
vars.put(“image”,result);

In CSV Data Set Config file contain all the image names and i have put this file in jmeter bin folder then jmeter will fetch the name of the images from this file with help of the parameter “FileNameFP” which was mentioned in CSV Data Set Config.

After this, the code mentioned above was convert the images into Base64 format and pass them into the request dynamically in every iteration.

Please help me Guys…

public static String imageEncodetoBase64(File inputImage) throws IOException {

    String base64String;
    byte[] fileContent = FileUtils.readFileToByteArray(inputImage.getAbsoluteFile());
    base64String = Base64.getEncoder().encodeToString(fileContent);
    return base64String ;
}

Hope this helps. you need to pass the path of image file