jmeter处理request和response

需求:

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

先给大家看结果:

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

 

 

实现代码:

BeanShell PreProcessor

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

Arguments args = sampler.getArguments(); // 截获请求参数部分
Argument arg_body = args.getArgument(0); // 获取请求body
String body = arg_body.getValue();  // 获取body的值保存成字符串

vars.put("test",body);

BeanShell PostProcessor

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));

 

posted @ 2020-05-23 00:00  Mosicol  Views(258)  Comments(0Edit  收藏  举报