如何在Postman中提取JSON响应的personReference并参数化到后续请求
Postman提取personReference字段参数化操作方案
操作步骤
- 打开对应请求的「Tests」标签页,输入以下脚本:
// 1. 解析接口返回的JSON数据 const resJson = pm.response.json(); // 2. 提取返回内容里的完整URL const targetUrl = resJson.result; // 3. 解析URL并提取personReference参数值 const urlObj = new URL(targetUrl); const personReferenceVal = urlObj.searchParams.get("personReference"); // 4. 将值存入环境变量,供后续请求调用 pm.environment.set("personReference", personReferenceVal); // 若需要使用URL解码后的原始值,可补充以下代码: // const decodedVal = decodeURIComponent(personReferenceVal); // pm.environment.set("decoded_personReference", decodedVal);
- 发送请求后,
personReference的取值会自动存入当前选中的Postman环境变量中 - 后续请求需要用到该字段时,直接通过占位符
{{personReference}}调用即可
结果验证
你可以在上述脚本末尾添加一行打印代码,确认提取结果是否正确:
console.log("提取到的personReference值:", pm.environment.get("personReference"));
发送请求后点击Postman左下角的「Console」按钮,即可在控制台查看打印的提取结果。
内容的提问来源于stack exchange,提问作者Bimsara Kasthure Arachchi
相关产品推荐
相关产品推荐

