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

发布于:2025-02-28 ⋅ 阅读:(11) ⋅ 点赞:(0)

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

假设接口返回以下数据:

{
    "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 标签中编写脚本:
在这里插入图片描述

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

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


运行第一个接口,可以在postman控制台查看结果
在这里插入图片描述

在第二个接口中调用

先查看环境变量中是否存在:
在这里插入图片描述
接口调用方法{{变量名}}
在这里插入图片描述