我在JMeter Pure java中介绍如何在httpSampler中设置content-type application/json
?
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("localhost");
httpSampler.setPort(8080);
httpSampler.setPath("/posts");
httpSampler.setMethod("POST");
httpSampler.setName("API");
String data = "{\"items\":[{\"stacking_limit\":null,\"id\":\"MLA60428354\",\"weight\":300,\"height\":20,\"only_rotate_axis\":null,\"width\":35,\"length\":45,\"quantity\":1}],\"pack\":{\"weight\":2000,\"height\":100,\"width\":100,\"length\":100}}";
httpSampler.addNonEncodedArgument("", data, "");
httpSampler.setPostBodyRaw(true);
HeaderManager headerManager = new HeaderManager();
Header h = new Header("Content-Type", "application/json");
headerManager.add(h);
httpSampler.setHeaderManager(headerManager);
当我向服务器发送帖子时,我会检查标题是否包含application/x-www-form-urlencoded
,如何更改?
答案 0 :(得分:0)
在Five Ways To Launch a JMeter Test without Using the JMeter GUI文章的讨论中提出了类似的问题,这是解决方案的基本部分:
// Create Header Manager
HeaderManager manager = new HeaderManager();
manager.add(new Header("Content-Type", "application/json"));
manager.setName(JMeterUtils.getResString("header_manager_title"));
manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
// Create HTTP Sampler
HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
httpSampler.setDomain("localhost");
httpSampler.setPort(8080);
httpSampler.setPath("/posts");
httpSampler.setMethod("POST");
httpSampler.setName("API");
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
//Add HTTP Header Manager to HTTP Sampler
HashTree httpSamplerTree = new HashTree();
httpSamplerTree.add(httpSampler, manager);