后置脚本
请求发送后执行的脚本,可获取响应参数和对参数进行处理等。
示例1:如获取msg中applyId的值
#接口响应
{
"code": 200,
"msg": "ApplySaveObject(applicantId=100485, projectId=37, positionId=966, buId=529, workPlaceId=1, applySrc=null, interviewCityId=11, recommendCode=112, mobile=13777898989, email=urstest_try@163.com, applicantName=张起灵, resumeId=1262, buId2=null, workPlaceId2=null, interviewCityId2=null, informationSource=1, informationSourceRemark=null, answerList=[ApplicantApplyResumeAnswer(id=null, applyId=1106421, questionId=188, answer=283, createTime=null, updateTime=null)])",
"data": null,
"rel": false,
"ok": false
}
在后置脚本做处理,此处使用的是2次分割的方法
#获取响应中msg
def bodyVar = api.http.response.body.get("msg");
#设置返回体数据,此处使用语法split('applyId='进行第一次分割,再','进行第二次分割得到id值)
api.http.response.body.put("applyId", bodyVar.split("applyId=")[1].split(",")[0]);
示例2:将获取msg的字符串转化成json格式
#接口响应
{
"code": 200,
"data": "{\n \"1\":{\n \"title\":\"限时购1\",\n \"subTitle\":\"限时秒杀升级\",\n \"tag\":\"天天1元秒\",\n \"url\":\"http://test.m.you.163.com/act/pub/IsV1O5l6zA2C.html?appConfig=1_1_1\",\n \"bkgGradientStartColor\":\"#FFE0E4\",\n \"bkgGradientEndColor\":\"#ffffff\"\n },\n \"2\":{\n \"title\":\"新品首发\",\n \"subTitle\":\"每日上新\",\n \"tag\":\"低价尝鲜\",\n \"tags\":[\"低价尝鲜1\",\"低价尝鲜2\", \"低价尝鲜3\", \"低价尝鲜4\", \"低价尝鲜5\", \"低价尝鲜6\", \"低价尝鲜7\"],\n \"days\":30,\n \"url\":\"https://test.m.you.163.com/act/pre/kGCEv9eW06fl.html\",\n \"desc\":\"app中模块已由9.9专区替换\",\n \"bkgGradientStartColor\":\"#FFE5CB\",\n \"bkgGradientEndColor\":\"#ffffff\"\n },\n \"3\":{\n \"title\":\"严选榜单\",\n \"subTitle\":\"跳转居家榜单\",\n \"subTitles\":[\"方真1\",\"方真2\", \"方真3\"],\n \"url\":\"https://m.you.163.com/item/saleRank\"\n },\n \"5\":{\n \"itemIds\":[470187517, 470192752, 10350000]\n }\n}"
}
#引用groovy.json包(用于JSON序列化及解析工作)
import groovy.json.JsonSlurper;
#将获取到的data字符串转成json
def respData= new JsonSlurper().parseText(api.http.response.body.data);
#将转义后的数据打印
api.http.response.body.put("apolloy", respData);