Atul
Atul

Reputation: 1774

JMeter - Add Custom HTTP Headers Programatically in HTTPSampler

I am addding custom headers to HTTPSampler programmatically while setting up JMeter test plan.

Please refer below snippet for the same:

HeaderManager headerManager = new HeaderManager();
headerManager.add(new Header("Foo", "Joe"));
sampler.setHeaderManager(headerManager);`

The problem here is that this custom header is not being sent to server.

Is there anything that I am missing here?

Upvotes: 2

Views: 2123

Answers (1)

Dmitri T
Dmitri T

Reputation: 168042

It won't work this way, you need to amend your code to add HeaderManager HashTree to HTTPSamplerProxy HashTree. After that you need to add this HTTPSamplerProxy to Thread Group, something like:

HashTree httpRequestTree = new HashTree();
httpRequestTree.add(httpRequest, manager);

testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpRequestTree);

Full code, just in case:

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

// Create Header Manager
HeaderManager manager = new HeaderManager();
manager.add(new Header("Foo", "Joe"));
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 - open example.com
HTTPSamplerProxy httpRequest = new HTTPSamplerProxy();
httpRequest.setDomain("example.com");
httpRequest.setPort(80);
httpRequest.setPath("/");
httpRequest.setMethod("GET");
httpRequest.setName("Open example.com");
httpRequest.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpRequest.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 httpRequestTree = new HashTree();
httpRequestTree.add(httpRequest, manager);

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

Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI for more information on building JMeter Test Plan programatically.

Upvotes: 1

Related Questions