Postman预请求体自动生成转义反斜杠问题的解决求助
解决Postman预请求中JSON转义反斜杠问题
问题根源
你当前的代码错误地将多个系统名称拼接成了带引号的单个字符串(比如"CFT","OPENWAY_NEW"),当JSON.stringify处理这个字符串时,会自动转义其中的双引号,导致生成的请求体出现\"转义符,最终引发请求失败。
修复方案
核心思路是让subSystems直接作为数组存储系统名称,而非拼接成带引号的字符串。修改后的完整预请求脚本如下:
// 获取集合变量 const sourceSystem = pm.collectionVariables.get("sourceSystem"); const targetSystem = pm.collectionVariables.get("targetSystem"); // 构建subSystems数组 let subSystems = []; if (sourceSystem === targetSystem) { subSystems.push(sourceSystem); } else { subSystems.push(sourceSystem, targetSystem); } // 构造请求体对象 const productsList = { "productTypes": ["ALL"], "forceUpdate": false, "subSystems": subSystems }; // 配置预请求参数 const portfolioProductsRequest = { url: pm.variables.get("url"), method: "POST", header: { "X-Initiator-Service": "orchestrator", "x-channel": "www2", "Content-Type": "application/json", "X-Mdm-Id": pm.variables.get("mdmId") }, body: JSON.stringify(productsList) };
修复说明
- 移除了冗余的
productSet变量,直接通过判断逻辑生成subSystems数组:- 当
sourceSystem和targetSystem相同时,数组仅包含一个系统名称 - 当二者不同时,数组直接包含两个系统名称
- 当
- 不再手动拼接引号,
JSON.stringify会自动处理数组的JSON序列化,生成正确的请求体格式
修改后,生成的请求体将变为:
{"productTypes":["ALL"],"forceUpdate":false,"subSystems":["CFT","OPENWAY_NEW"]}
内容的提问来源于stack exchange,提问作者Igor Martakov
相关产品推荐
相关产品推荐

