Tho*_*odi 2 postman postman-pre-request-script
我想在 JSON 正文中使用注释来进行快速引用,如下所示:
{
/**
* some param info
* param: [
* 1 = value so
* 2 = value that
* ]
*/
"param": [
1
],
/* some param2 info */
"param2": "value",
// pls use this
"param3": [
"values"
//"home"
]
}
Run Code Online (Sandbox Code Playgroud)
在请求发送到服务器之前应该过滤掉评论。
最后,从 Postman v8.3.0 开始,您可以在集合pre-request脚本中执行此操作:
// Strip JSON Comments
if (pm?.request?.body?.options?.raw?.language === 'json') {
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)));
}
Run Code Online (Sandbox Code Playgroud)
这会从 json 中删除所有注释,并将当前正文设置为已清理的正文,此代码所基于的原始 github 帖子中有更多其他正文类型(GraphQL、URL 编码、表单数据)的示例。