Olya Muravjova
Olya Muravjova

Reputation: 13

Adding header programmatically using Jmeter Java API

I'm trying to send http post request with json using Jmeter API. I'm adding HeaderManager with content-type header, but it seems not to be working as I expect, because I'm getting the request with Content-Type: application/x-www-form-urlencoded

    HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();
    httpSamplerProxy.setDomain("localhost");
    httpSamplerProxy.setPort(8090);
    httpSamplerProxy.setPath("/");
    httpSamplerProxy.setMethod("POST");
    httpSamplerProxy.addEncodedArgument("Body Data", "{\"1\":\"2\"}", "");
    HeaderManager headerManager = new HeaderManager();
    headerManager.add(new Header("Content-type", "application/json"));
    httpSamplerProxy.setHeaderManager(headerManager);

What do I do wrong? How to add a header to httpSampler?

Upvotes: 1

Views: 3131

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

You have several issues with your test:

  1. Using API is not recommended way of creating a JMeter test as API might change hence your test may become fragile and won't survive upgrade to next JMeter version. You should be creating tests using JMeter GUI and once done you have freedom to run it using whatever approach.
  2. You should not be running JMeter and application under test on the same host to avoid mutual interference, JMeter tests can be very resource intensive.
  3. "Type" word in the Content-Type header name should be upper-case, it matters.

If you are still looking for the way of adding HTTP Header Manager programmatically, as per one of comments to the Five Ways To Launch a JMeter Test without Using the JMeter GUI you should be doing it a little bit differently, to wit:

// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();

// Create Header Manager
HeaderManager manager = new HeaderManager();
manager.add(new Header("Content-Type", "application/json"));
manager.setName(JMeterUtils.getResString("header_manager_title")); // $NON-NLS-1$
manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());

// HTTP Sampler 
HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();
httpSamplerProxy.setDomain("localhost");
httpSamplerProxy.setPort(8090);
httpSamplerProxy.setPath("/");
httpSamplerProxy.setMethod("POST");
httpSamplerProxy.setName("HTTP Request");
httpSamplerProxy.addEncodedArgument("Body Data", "{\"1\":\"2\"}", "");
httpSamplerProxy.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSamplerProxy.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
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("Example 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());

// HTTP Request Sampler and Header Manager
HashTree requestHashTree = new HashTree();
requestHashTree.add(httpSamplerProxy, manager);

// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(requestHashTree);

Upvotes: 2

Related Questions