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控制台查看结果

在第二个接口中调用

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

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

相关推荐
梧桐16811 小时前
14天、31个缺陷、28个测试脚本—用 Claude Code 完成第三方软件测试
人工智能·测试工具
麻瓜code13 小时前
秒杀优化三连:Lua + 阻塞队列 + Redis Stream,响应从 3.4 秒到毫秒级
redis·junit·lua
努力努力再努力wz13 小时前
【Lua 入门系列】Lua 函数深入理解:函数对象、多返回值、可变参数、闭包、递归与尾调用优化
开发语言·lua
hey you~17 小时前
云呼叫中心坐席自动分配实战:ACD 策略、Redis+Lua 实现与排查清单
数据库·redis·lua·云呼叫中心·acd·坐席分配·坐席状态机
我不会起名字32218 小时前
Redis pipeline 用了反而更慢:3 类误用与正确的批量姿势
redis·pipeline·lua·rtt·批量
mewowaBc19 小时前
postman使用细则
测试工具·postman·偏差
光影6271 天前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率
xie881018022 天前
正则里的点号不匹配换行:多行模式的开关
运维·服务器·测试工具
努力努力再努力wz2 天前
【Lua入门系列】从 C++ 迭代器到 Lua 迭代器:一文深入理解 Lua 有状态以及无状态迭代器与泛型for、闭包、ipairs、pairs 和 next
开发语言·c++·lua
屈冠成3 天前
ShopXO商城系统测试报告
开发语言·spring·maven·lua