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

在第二个接口中调用

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

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

相关推荐
软件测试-阿涛10 小时前
【性能测试】Jmeter+Grafana+InfluxDB+Prometheus Windows安装部署教程
测试工具·jmeter·性能优化·压力测试·grafana·prometheus
慢慢沉19 小时前
Lua(数据库访问)
开发语言·数据库·lua
慢慢沉19 小时前
Lua协同程序(coroutine)
lua
惜.己1 天前
pytest中使用skip跳过某个函数
开发语言·python·测试工具·pytest
绿色果酱1 天前
Apipost:离线可用+AI全栈覆盖,国产API开发协作工具新标杆
postman·ai功能·apipost·apipost离线·apipost ai·ai能力·apipost操作
慧都小项1 天前
自动化UI测试工具TestComplete的AI双引擎:即时数据集 + 自愈测试
自动化测试·测试工具·llm·数据驱动测试·hipaa标准
alien爱吃蛋挞1 天前
Postman
测试工具·postman
慢慢沉2 天前
Lua元表(Metatable)
lua
程序员小远2 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest