jmeter处理request和response

需求:

同时打印出:请求体(json)和响应体(json)

先给大家看结果:

如下图:可见请求体和返回体同时打印出来了

实现代码:

BeanShell PreProcessor

import org.json.*;
import org.apache.jmeter.config.*; 

Arguments args = sampler.getArguments(); // 截获请求,包含url、headers 和 body 三部分
Argument arg_body = args.getArgument(0); // 获取请求body
String body = arg_body.getValue();  // 获取body的值保存成字符串

vars.put("test",body);

BeanShell PostProcessor

扫描二维码关注公众号,回复: 11249748 查看本文章
import org.json.*;

String response = prev.getResponseDataAsString();

JSONObject jsonObject = new JSONObject(response);


String reqs = vars.get("test");
log.info("[request]:  "+ reqs);
//jsonObject.toString(4):格式化输出为了好看一点
log.info("[response]: "+ jsonObject.toString(4));

猜你喜欢

转载自www.cnblogs.com/mosicol/p/12940507.html