目录

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

在第二个接口中调用

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

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

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
niuniu_6662 小时前
针对 Python 3.7.0,以下是 Selenium 版本的兼容性建议和安装步骤
开发语言·chrome·python·selenium·测试工具
可涵不会debug10 小时前
JMeter接口性能测试从入门到精通
selenium·测试工具·jmeter·压力测试
爱的叹息15 小时前
Redis 除了数据类型外的核心功能 的详细说明,包含事务、流水线、发布/订阅、Lua 脚本的完整代码示例和表格总结
数据库·redis·lua
一个程序员(●—●)1 天前
xLua环境控制+xLua的Lua调用C#的1
开发语言·unity·c#·lua
lisw051 天前
AI驱动的测试自动化:智能测试工具全景评测
人工智能·测试工具·自动化
独隅1 天前
针对Ansible执行脚本时报错“可执行文件格式错误”,以下是详细的解决步骤和示例
运维·开发语言·ansible·lua·lua5.4
可可南木1 天前
BT-Basic函数之首字母T
开发语言·测试工具·pcb工艺
时光话2 天前
Lua:第1-4部分 语言基础
开发语言·lua
炬火初现2 天前
redis-cpp-cpp如何使用lua脚本
数据库·redis·lua
cuijiecheng20182 天前
音视频入门基础:MPEG2-PS专题(8)——使用Wireshark分析GB28181的PS流
测试工具·wireshark·音视频