title: How to add the request body data in the post request for JMeter testing using Java code in the non-GUI mode?
body: <p>This is a Jmeter POST request in the form of a java code. I run this code to get no errors but I also get no response because my method of acting the request body data appears to be wrong.    </p>
  
  <pre><code>import java.io.FileOutputStream;
  
  import org.apache.jmeter.config.Arguments;
  import org.apache.jmeter.config.gui.ArgumentsPanel;
  import org.apache.jmeter.control.LoopController;
  import org.apache.jmeter.control.gui.LoopControlPanel;
  import org.apache.jmeter.control.gui.TestPlanGui;
  import org.apache.jmeter.engine.StandardJMeterEngine;
  import org.apache.jmeter.protocol.http.control.Header;
   import org.apache.jmeter.protocol.http.control.HeaderManager;
  import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
  import org.apache.jmeter.protocol.http.gui.HeaderPanel;
  //import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
  import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
  import org.apache.jmeter.reporters.ResultCollector;
  import org.apache.jmeter.reporters.Summariser;
  //import org.apache.jmeter.samplers.SampleEvent;
  //import org.apache.jmeter.samplers.SampleResult;
  import org.apache.jmeter.samplers.SampleSaveConfiguration;
  import org.apache.jmeter.save.SaveService;
  import org.apache.jmeter.testelement.TestElement;
  import org.apache.jmeter.testelement.TestPlan;
  import org.apache.jmeter.threads.ThreadGroup;
  import org.apache.jmeter.threads.gui.ThreadGroupGui;
  import org.apache.jmeter.util.JMeterUtils;
  import org.apache.jorphan.collections.HashTree;
  import org.apache.jmeter.protocol.http.util.HTTPArgument;
  
  
  public class PostRequest {
  
  public static void main(String[] argv) throws Exception {
      //JMeter Engine
      StandardJMeterEngine jmeter = new StandardJMeterEngine();
  
      //JMeter initialization (properties, log levels, locale, etc)
      JMeterUtils.setJMeterHome("/C:/Users/xxx");
      JMeterUtils.loadJMeterProperties("/C:/xxx");
      JMeterUtils.initLogging();
      JMeterUtils.initLocale();
  
      //JMeter Test Plan, basically JOrphan HashTree
      HashTree testPlanTree = new HashTree();
  
      //HTTP Sampler
      HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
      httpSampler.setDomain("100.100.100.100");
      httpSampler.setPort(1111);
      httpSampler.setPath("/");
      httpSampler.setMethod("POST");
      httpSampler.addNonEncodedArgument("","{data:{ \"email\" : 
  \"xx@gmail.com\",there are more values in the body"}]}","" );
      httpSampler.setPostBodyRaw(true);
  
      httpSampler.setFollowRedirects(true);
      httpSampler.setAutoRedirects(false);
      httpSampler.setUseKeepAlive(true);
      httpSampler.setDoMultipartPost(false);
      httpSampler.setProperty(TestElement.TEST_CLASS, 
  HTTPSamplerProxy.class.getName());
      httpSampler.setProperty(TestElement.GUI_CLASS, 
  HttpTestSampleGui.class.getName());
  
      //Header Manager
      HeaderManager headerManager = new HeaderManager();
  
      headerManager.setName(JMeterUtils.getResString("header_manager_title")); 
      headerManager.add(new Header("abcs", "asdasd"));
      headerManager.add(new Header("dsferdg", "ertret"));
      headerManager.setProperty(TestElement.TEST_CLASS, 
  HeaderManager.class.getName());
      headerManager.setProperty(TestElement.GUI_CLASS, 
  HeaderPanel.class.getName());
      httpSampler.setHeaderManager(headerManager);
  
      //Loop Controller
      LoopController loopController = new LoopController();
      loopController.setLoops(1);
      loopController.setFirst(true);
      loopController.addTestElement(httpSampler);
      loopController.setProperty(TestElement.TEST_CLASS, 
  LoopController.class.getName());
      loopController.setProperty(TestElement.GUI_CLASS, 
  LoopControlPanel.class.getName());
      loopController.initialize();
  
      //Thread Group
      ThreadGroup threadGroup = new ThreadGroup();
      threadGroup.setName("Sample Thread Group");
      threadGroup.setNumThreads(1);
      threadGroup.setRampUp(1);
      threadGroup.setSamplerController(loopController);
      threadGroup.setProperty(TestElement.TEST_CLASS, 
  ThreadGroup.class.getName());
      threadGroup.setProperty(TestElement.GUI_CLASS, 
  ThreadGroupGui.class.getName());
  
      //Test Plan
      TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
      testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
      testPlan.setProperty(TestElement.GUI_CLASS, 
  TestPlanGui.class.getName());
      testPlan.setUserDefinedVariables((Arguments) new 
  ArgumentsPanel().createTestElement());
  
      HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
      threadGroupHashTree.add(httpSampler,headerManager);
  
      //Construct Test Plan from previously initialized elements
      testPlanTree.add("testPlan", testPlan);
      testPlanTree.add("loopController", loopController);
      testPlanTree.add("threadGroup", threadGroup);
      testPlanTree.add("httpSampler", httpSampler);
      testPlanTree.add("headerManager", headerManager);
  
      Summariser summer = null;
      String summariserName = JMeterUtils.getPropDefault("summariser.name", 
  "summary");
      if (summariserName.length() &gt; 0) {
          summer = new Summariser(summariserName);
              }
  
  
      ResultCollector resultcoll = new ResultCollector();
      testPlanTree.add("resultcoll", resultcoll);
      resultcoll.setFilename("C:/Users/xxx");
      SampleSaveConfiguration saveConfiguration = new 
  SampleSaveConfiguration();
      saveConfiguration.setAsXml(true);
      saveConfiguration.setCode(true);
      saveConfiguration.setLatency(true);
      saveConfiguration.setTime(true);
      saveConfiguration.setTimestamp(true);
      resultcoll.setSaveConfig(saveConfiguration);
  
      //Store execution results into a .jtl file, we can save file as csv also
      String reportFile = "C:/Users/xxx.jtl";
      String csvFile = "C:/Users/xxx.csv";
      ResultCollector logger = new ResultCollector(summer);
      logger.setFilename(reportFile);
      ResultCollector csvlogger = new ResultCollector(summer);
      csvlogger.setFilename(csvFile);
      testPlanTree.add(testPlanTree.getArray()[0], logger);
      testPlanTree.add(testPlanTree.getArray()[0], csvlogger);
  
      //save generated test plan to JMeter's .jmx file format
      SaveService.saveTree(threadGroupHashTree, new 
  FileOutputStream("C:\\Users\read.jmx"));
  
      // Run Test Plan
      jmeter.configure(testPlanTree);
      jmeter.run();
  
      /*System.out.println("Test completed. See " + JMeterHome + slash + 
  "report.jtl file for results");
      System.out.println("JMeter .jmx script is available at " + jmeterHome + 
  slash + "jmeter_api_sample.jmx");*/
      System.exit(0);
  }
  
  }
  </code></pre>
  
  <p><em>I have used this code to configure the jmeter POST request and to add the body data but the request body appears to be <code>null</code> when I check the response.</em></p>