Search code examples
javajmeterperformance-testingbeanshell

How to capture response of a Bean shell sampler


enter image description here

Above is my code i have written to hit a web service whose endpoint is expecting a Byte Stream object. I am able to do that but i am not getting any response. I have to test the response. Though i am getting 200 ok but a string is sent in response that i am not getting. enter image description here

enter image description here

And the response is blank enter image description here

How can I get the response ?


Solution

    1. In order to read server's response you need to use URLConnection.getInputStream() method, not OutputStream
    2. In order to convert stream to string you can use IOUtils.toString() method
    3. In order to return data you can use return keyword
    4. Minimal working code is below, adjust as per your needs:

      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.nio.charset.StandardCharsets;
      import org.apache.commons.io.IOUtils;
      
      URL url = new URL("http://example.com");
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      con.setRequestMethod("GET");
      String response = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8);
      return response;