0

Setting Content-Type application/json in HeaderManager and passing to Jmeter, but is being overriden while being called as text/plain

            HeaderManager headerManager = new HeaderManager();
            headerManager.add(createHeader("Content-Type", "application/json"));

            HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
            httpSampler.setProtocol("https");
            httpSampler.setDomain(config.getString("JMETER_DOMAIN"));
            httpSampler.setPath(api);
            httpSampler.setMethod(method);

            TestPlan testPlan = new TestPlan("MY TEST PLAN");

            // Add elements to HashTree
            ListedHashTree testPlanTree = new ListedHashTree();
            testPlanTree.add(testPlan);
            ListedHashTree threadGroupTree = new ListedHashTree(threadGroup);
            threadGroupTree.add(httpSampler,headerManager);
            testPlanTree.add(testPlan, threadGroupTree);            
            // Run Test Plan
            jm.configure(testPlanTree);
            jm.run();

O/p

14:26:57.653 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /auth/Authentication/login HTTP/1.1
14:26:57.653 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: close
14:26:57.653 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 33
14:26:57.653 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Type: text/plain
14:26:57.653 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: example.com
14:26:57.653 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.14 (Java/1.8.0_372)
14:26:57.672 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 415 Unsupported Media Type
14:26:57.672 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Tue, 18 Jul 2023 14:26:57 GMT
14:26:57.672 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: application/problem+json; charset=utf-8
14:26:57.672 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 << Transfer-Encoding: chunked
14:26:57.672 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: close
14:26:57.672 [ 1-1] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Kestrel
14:26:57.680 [ 1-1] INFO  org.apache.jmeter.threads.JMeterThread - Thread is done:  1-1
14:26:57.680 [ 1-1] INFO  org.apache.jmeter.threads.JMeterThread - Thread finished:  1-1

How do i change the content-type as per my requirement and avoid jmeter overriding it

Tried using HeaderManager but no luck yet

  • If I'm not mistaken HttpHeaderManager should be part of TestPlan, you can refer this answer https://stackoverflow.com/questions/13032753/jmeter-how-to-send-request-with-content-type-header – Anton Tokmakov Jul 18 '23 at 15:26

1 Answers1

0

I don't have any issues with the following code:

HeaderManager headerManager = new HeaderManager();
headerManager.add(new Header("Content-Type","application/json"));
headerManager.setName("HTTP Header Manager");
headerManager.setProperty(TestElement.TEST_CLASS,HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());

so take a look at your createHeader() function, most probably the problem is somewhere there.

You can also add the following line:

SaveService.saveTree(testPlanTree, Files.newOutputStream(Paths.get("/path/to/test.jmx)));

and open the test.jmx in JMeter GUI and ensure that the HTTP Header Manager is created and the HTTP Request sampler is in its scope.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133