当前位置:   article > 正文

postman将接口返回结果生成json文件到本地_postman导出json文件

postman导出json文件

postman将接口返回的结果除了可以【生成csv文件】到本地还可以生成json文件。具体操作如下:

一样的模拟接口返回批量参数值,测试代码如下:

@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {

    @PostMapping("/testGetParams")
    private BizResponse<List<WarehouseDto>> testGetParams() {
        List<WarehouseDto> warehouseDtoList = new ArrayList<>();
        WarehouseDto warehouseDto1 = new WarehouseDto();
        warehouseDto1.setId(1L);
        warehouseDto1.setDescription("test1");
        warehouseDtoList.add(warehouseDto1);

        WarehouseDto warehouseDto2 = new WarehouseDto();
        warehouseDto2.setId(2L);
        warehouseDto2.setDescription("test2");
        warehouseDtoList.add(warehouseDto2);
        return ResponseUtil.success(warehouseDtoList);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

这个接口返回的数据结构如下:
{ "status":1, "code":"10000", "data":[ { "id":1, "description":"test1" }, { "id":2, "description":"test2" } ] }

postman中新建request,并测试将请求返回结果生成json文件,步骤:

1、添加接口请求url以及请求参数Body

这里是引用

2、在Tests中添加以下代码:
var data = pm.response.json().data;//拿到的这个数据:[ { "id":1, "description":"test1" }, { "id":2, "description":"test2" } ]
console.log(data);
var dataStr = '[';
for(var i=0;i<data.length;i++){
    dataStr+= '{"id":' + data[i].id  + ',"description":"' + data[i].description  + '"}' + (i==data.length-1?'':',');
}
dataStr+= ']';//只提取需要生成json文件的属性值,组装成数组
console.log(dataStr);
let opts = {
    requestName: request.name  || request.url,
    fileExtension: 'json',//这个和csv文件配置不一样
    mode: 'writeFile',//这个和csv文件配置不一样
    uniqueIdentifier: false,
    responseData: dataStr
};


pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(opts)
    }
}, function (err, res) {
    console.log(res);
});

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

其他的就是启动postman本地服务,(可参考生成csv文件配置本地服务步骤)。调用接口可以看到接口调用完后执行的write生成文件,在项目目录下C:\soft\ResponseToFile-Postman\Responses下可以看到生成的json文件

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/527295
推荐阅读
相关标签
  

闽ICP备14008679号