本文介绍关于Postman中Body添加注释后请求报错问题解决方法
如:请求返回下述报错
操作失败! 系统异常,JsonParseException: Unexpected character (‘/’ (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature ‘ALLOW_COMMENTS’ not enabled for parser)\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 6, column: 20
解决方法:
在Pre-request Scrip内加入下述代码块即可
// 去除请求body中的注释
if (pm?.request?.body?.mode === 'raw') {
const rawData = pm.request.body.toString();
const strippedData = rawData.replace(
/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
(m, g) => g ? "" : m
);
// pm.request.body.update(JSON.stringify(JSON.parse(strippedData)));
pm.request.body.raw = JSON.stringify(JSON.parse(strippedData));
}
创作不易,觉得有帮助的小伙伴儿来个一键三连呗!!!!