postman上一个接口返回值作为下一个接口的入参

1.在第一个接口中提取响应数据

假设接口返回以下数据:

c 复制代码
{
    "total": 2,
    "rows": [
        {
            "createBy": null,
            "createTime": "2024-07-21 12:54:24",
            "updateBy": null,
            "updateTime": "2024-09-09 15:03:31",
            "remark": "",
            "pageNum": null,
            "pageSize": null,
            "model": null,
            "deviceId": 134,
            "deviceName": "12345678",
            "deviceSecret": "389092c0e608ee50f6089f4fe445ecb2",
            "iotId": "YrYcf5lMY411Ygd4f5G6k0rs40",
            "regenInfo": null,
            "selfCheckRes": null,
            "nickName": "12345678",
            "productKey": "k0rs4YHsimt",
            "gmtActive": null,
            "gmtCreate": null,
            "region": "cn-shanghai",
            "status": "OFFLINE",
            "userId": "313073773593104384",
            "deviceError": null,
            "leakAlarm": null,
            "lowSaltWarn": null,
            "lbs": null,
            "regenTime": null,
            "workMode": null,
            "selfCheckSw": null,
            "hardWaterSw": 0,
            "reqtoRegenSw": 1,
            "hardness": null,
            "saltPer": null,
            "dailyUsedWater": null,
            "waterYield": null,
            "productName": "IOT软水机",
            "iotInstanceId": "iot-06z00bjektr6gff",
            "endpointOverride": "iot.cn-shanghai.aliyuncs.com",
            "fullAddress": null,
            "installationAddress": null,
            "userBoundTime": "2024-09-04 11:21:53",
            "userName": "微信用户e1609",
            "version": null,
            "gmtOnline": null,
            "ipAddress": null,
            "nodeType": 0,
            "regenCheckRes": null,
            "posHoldTime": null,
            "qrCodeAddress": "https://fobrite123.oss-cn-hangzhou.aliyuncs.com/qr_codes/qr_12345678.png",
            "userPhone": "15850559780",
            "residualFlow": null,
            "arrearsStatus": null,
            "firstBoundTime": "2024-09-04 11:21:51"
        },
        {
            "createBy": null,
            "createTime": "2024-07-20 09:59:37",
            "updateBy": null,
            "updateTime": "2024-09-14 14:15:55",
            "remark": "",
            "pageNum": null,
            "pageSize": null,
            "model": null,
            "deviceId": 130,
            "deviceName": "898608401024D0194097",
            "deviceSecret": "45b55bbfd571dc3ec7d50328c689e3dc",
            "iotId": "Zk5FvmYcQVT9mkYFDVNnk0rs40",
            "regenInfo": null,
            "selfCheckRes": "{\"Brine\": 1, \"Outlet\": 1, \"Sewage\": 1}",
            "nickName": "体验样机4",
            "productKey": "k0rs4YHsimt",
            "gmtActive": null,
            "gmtCreate": null,
            "region": "cn-shanghai",
            "status": "OFFLINE",
            "userId": "315173099693805568",
            "deviceError": null,
            "leakAlarm": null,
            "lowSaltWarn": null,
            "lbs": "{\"Lat\": 31.8419, \"Lng\": 118.8304}",
            "regenTime": "{\"Min\": 0, \"Days\": 7, \"Hour\": 2}",
            "workMode": 0,
            "selfCheckSw": 0,
            "hardWaterSw": 0,
            "reqtoRegenSw": 1,
            "hardness": 2,
            "saltPer": 0,
            "dailyUsedWater": 0,
            "waterYield": "{\"Nominal\": 1827, \"Surplus\": 1043, \"TodayWaterUse\": 0}",
            "productName": "IOT软水机",
            "iotInstanceId": "iot-06z00bjektr6gff",
            "endpointOverride": "iot.cn-shanghai.aliyuncs.com",
            "fullAddress": "前靖路1号,盛福花园",
            "installationAddress": "江苏省南京市江宁区",
            "userBoundTime": "2024-07-20 10:02:46",
            "userName": "微信用户e35b7",
            "version": "{\"Model\": \"IOT\", \"Hardware\": \"D240407\", \"Software\": \"24072001\"}",
            "gmtOnline": null,
            "ipAddress": null,
            "nodeType": 0,
            "regenCheckRes": "{\"Brine\": 1, \"Rinse\": 1, \"Refill\": 1, \"BackWash\": 1, \"Dissolve\": 1}",
            "posHoldTime": "{\"Brine\": 40, \"Rinse\": 2, \"Refill\": 5, \"Dissove\": 120, \"BackWash\": 1}",
            "qrCodeAddress": "https://fobrite123.oss-cn-hangzhou.aliyuncs.com/qr_codes/qr_898608401024D0194097.png",
            "userPhone": "13905141286",
            "residualFlow": null,
            "arrearsStatus": null,
            "firstBoundTime": "2024-07-20 10:02:45"
        }
    ],
    "code": 200,
    "msg": "查询成功"
}

在第一个接口的 Tests 标签中编写脚本:

c 复制代码
// 提取响应中的 deviceId并保存为环境变量
const jsonData = pm.response.json();
pm.environment.set("deviceId", jsonData.rows[0].deviceId); 

// 调试:打印变量值(通过 Postman Console 查看)
console.log("deviceId:", pm.environment.get("deviceId"));

运行第一个接口,可以在postman控制台查看结果

在第二个接口中调用

先查看环境变量中是否存在:

接口调用方法{{变量名}}

相关推荐
秃了也弱了。2 小时前
WireShark:非常好用的网络抓包工具
网络·测试工具·wireshark
敲上瘾4 小时前
Linux系统cgroups资源精细化控制基础
linux·测试工具·docker·压力测试·cgroups
天才测试猿10 小时前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
利来利往21 小时前
【ai写代码】lua-判断表是否被修改
lua
陈天cjq21 小时前
Redis 实用型限流与延时队列:从 Lua 固定/滑动窗口到 Streams 消费组(含脚本与压测)
redis·junit·lua
Warren9821 小时前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
测试开发Kevin2 天前
详解grafana k6 中stage的核心概念与作用
测试工具·压力测试·grafana
天才测试猿2 天前
接口自动化测试难点:数据库验证解决方案
自动化测试·软件测试·数据库·python·测试工具·职场和发展·接口测试
柯南二号2 天前
MacOS 系统计算机专业好用工具安装
开发语言·lua
神洛华2 天前
Lua语言程序设计2:函数、输入输出、控制结构
开发语言·lua