Select Page

When our Rest API tests fail, our developers need to view the full request body (not just the request url and params) from Jenkins. In this use case, the JMeter GUI is not a viable option for test results viewership.

We often use JMeter to perform functional testing of our Rest API. In this context, we’re not interested in performance. Rather, we’re interested in functionality of the Rest API and response body format. Note: we fully understand the performance and data size penalty incurred when logging the full request with request params and body. Back to our developers… While JMeter does allow for viewing the entire request via the JMeter GUI -> View Results Tree component, our developers aren’t viewing the test results from the JMeter GUI.

Our developers are looking at the tests executed by our CI / CD pipeline and so they navigate to the Jenkins job that kicked off the tests.

To enable developers to see the entire Request + Request Body for each request, we take advantage of a JMeter Listener.

try{
var message = "";
	var currentUrl = sampler.getUrl();
	message +=  ". URL = " +currentUrl;
	var requestBody = sampler.getArguments().getArgument(0).getValue();
 	message += " --data " + sampler.getArguments();
     //another option is to use ctx.getCurrentSampler()
     //log.info(ctx.getCurrentSampler());
	log.info(message)
	
}catch(err){
	//do nothing. this could be a debug sampler. no need to log the error
}

To log ONLY failures, try the following snippet instead

try{
  var message = "";
  var currentUrl = sampler.getUrl();
  message +=  ". URL = " +currentUrl;
  var requestBody = sampler.getArguments().getArgument(0).getValue();
  message += " --data " + sampler.getArguments();

  if(!sampleResult.isSuccessful()){
      log.error(message);
  }

}catch(err){
  //do nothing. this could be a debug sampler. no need to log the error
}

The end result is that the Jenkins job log file now contains every request URL and body.