Search code examples
jmeterbeanshell

How can I read a byte array in Jmeter from a database?


I have a MySQL database and am trying to get this byte array using JDBC Request. I am saving the result in a variable and then in a BeanShell encoding this result in a string and sending it through an HTTP POST:

BeanShell:

import java.net.URLEncoder;
import org.apache.commons.net.util.Base64;

String source = vars.get("storedToken_1"); 
byte[] encodedBytes = Base64.encodeBase64(source.getBytes()); 
String encoded = new String(encodedBytes); 
String code = URLEncoder.encode(encoded, "UTF-8");
vars.put("reset_pass_token", code);

The thing is that this storedToken I'm getting from the db is coming corrupted so I'm not being able to authenticate the test.

Is there any other way I can retrieve this byte array value from the database and store it in a variable so I can send it in a POST?


Solution

  • If the variable comes as byte array, you need to handle it a little bit differently in Beanshell using vars.getObject() method, like:

    String source = new String(vars.getObject("storedToken_1"));
    

    Also the way you extract the variable may matter, JMeter won't "corrupt" the data, it might be the case that you misuse JDBC Test Element. See Debugging JDBC Sampler Results in JMeter article for available options listed and if it doesn't help - update your question with screenshot or detailed description of your JDBC element configuration.