Search code examples
web-servicesjmeterbase64automated-testsjmeter-plugins

How to encode an attachment to Base64 in Jmeter?


I'm trying to encode a file to Base64 in Jmeter to test a web service using the following script:

String file = FileUtils.readFileToString(new File("${filepath}"), "UTF-8");
vars.put("file", new String(Base64.encodeBase64(file.getBytes("UTF-8"))));

This works fine for plain/text files and does not work for other file types correctly.

How could I make it work for other file types?


Solution

  • Jmeter has many potions to convert a variable to "Base64", below are a few options

    1. Bean shell pre processor
    2. BeanShell PostProcessor
    3. BeanShell Sampler.

    Below is the "bean shell" code, which used in "Bean shell pre processor" to convert variable to Base64

    import org.apache.commons.codec.binary.Base64;
    
    String emailIs= vars.get("session");
    
    byte[] encryptedUid = Base64.encodeBase64(emailIs.getBytes());
    vars.put("genStringValue",new String(encryptedUid));
    

    Example :

    Before Base64 :jdwqoeendwqqm12sdw

    After Base64 :amR3cW9lZW5kd3FxbTEyc2R3

    Converted using Jmeter :

    enter image description here

    enter image description here

    enter image description here Converted Using base64 site:

    enter image description here