如何在 Postman JSON 正文中添加评论(用于集合级别的预请求脚本) [英] How to add comments in Postman JSON body (for collection level pre-request script)

查看:34
本文介绍了如何在 Postman JSON 正文中添加评论(用于集合级别的预请求脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 JSON 正文中使用注释来快速引用,如下所示:

I want to use comments within my JSON body for quick references like this:

{
    /**
    * some param info
    *   param: [
    *     1 = value so
    *     2 = value that
    *   ]
    */
    "param": [
        1
    ],
    /* some param2 info */
    "param2": "value",
    // pls use this
    "param3": [
        "values"
        //"home"
    ]
}

在将请求发送到服务器之前,应过滤掉评论.

The comments should be filtered out before the request is getting sent to the server.

推荐答案

最后,从 Postman v8.3.0 开始,您可以在集合 pre-request 脚本中执行此操作:

Finally, since Postman v8.3.0 you can do this in your collections pre-request script:

// Strip JSON Comments
if (pm?.request?.body?.options?.raw?.language === 'json') {
    const rawData = pm.request.body.toString();
    const strippedData = rawData.replace(
        /\"|"(?:\"|[^"])*"|(//.*|/*[sS]*?*/)/g,
        (m, g) => g ? "" : m
    );
    pm.request.body.update(JSON.stringify(JSON.parse(strippedData)));
}

这会从 json 中删除所有注释并将当前正文设置为已清理的正文,在 原始 github 帖子 此代码基于.

This strips all comments from the json and sets the current body to the cleaned one, there are more examples for other body types (GraphQL, URL Encoded, Form Data) in the original github post this code is based from.

这篇关于如何在 Postman JSON 正文中添加评论(用于集合级别的预请求脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆