从预请求体中移除自动反斜杠

huangapple go评论67阅读模式
英文:

Remove auto backslashes from pre-request body

问题

我想为检查客户帐户的特殊系统进行预请求。

我设置了变量sourceSystem和targetSystem。
然后Postman检查系统名称的相似性。

if (pm.collectionVariables.get("sourceSystem") === pm.collectionVariables.get("targetSystem")) {
    pm.collectionVariables.set("productSet", pm.collectionVariables.get("sourceSystem"))
} else {
    pm.collectionVariables.set("productSet", pm.collectionVariables.get("sourceSystem") + '","' + pm.collectionVariables.get("targetSystem"))
};

正如您在else语句中所看到的,我得到了额外的引号和系统名称之间的逗号。否则当然会出错。
之后,我将变量声明为请求的json主体,其中包含两个系统在subSystems数组中。

productsList = {
    "productTypes": [
        "ALL"
    ],
    "forceUpdate": false,
    "subSystems": [
        pm.collectionVariables.get("productSet")
    ]
};

然后是预请求的最终设置。

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: pm.variables.replaceIn(JSON.stringify(productsList))
};

在pm.sendRequest之后,我在日志中得到了它。

{"productTypes":["ALL"],"forceUpdate":false,"subSystems":["CFT","OPENWAY_NEW"]}

有两个反斜杠,它们破坏了我的请求。

我得到的主要问题在于设置"productSet"。 Postman将字符串放在最后。在字符串内部,它应该标记引号。

要使请求正确,而不生成自动生成的反斜杠,您应该更改如下:

在设置"productSet"的地方,将引号添加到数组中,以便不需要自动生成反斜杠。

if (pm.collectionVariables.get("sourceSystem") === pm.collectionVariables.get("targetSystem")) {
    pm.collectionVariables.set("productSet", [pm.collectionVariables.get("sourceSystem")])
} else {
    pm.collectionVariables.set("productSet", [pm.collectionVariables.get("sourceSystem"), pm.collectionVariables.get("targetSystem")])
};

这样做后,"productSet"将是一个包含系统名称的数组,而不是一个逗号分隔的字符串,就不会生成额外的反斜杠。

英文:

I want to make a pre-request for checking client's accounts for special systems.

I set variables sourceSystem and targetSystem.
Then Postman checks the similarity of systems' names.

if(pm.collectionVariables.get("sourceSystem")===pm.collectionVariables.get("targetSystem")){
    pm.collectionVariables.set("productSet", pm.collectionVariables.get("sourceSystem"))
}else{
    pm.collectionVariables.set("productSet", pm.collectionVariables.get("sourceSystem") +'"' +","+ '"' + pm.collectionVariables.get("targetSystem"))
};

As you can see in else statement I've got additional quotes and a comma between systems' names. Otherwise it will be broken of course.
After that I declare variable as a json body for request with two systems in subSystems array.

productsList = {
    "productTypes": [
        "ALL"
    ],
    "forceUpdate": false,
    "subSystems": [
        pm.collectionVariables.get("productSet")
    ]
};

And the final settings of pre-request

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: pm.variables.replaceIn(JSON.stringify(productsList))
};

After pm.sendRequest I've got it in log.

{"productTypes":["ALL"],"forceUpdate":false,"subSystems":["CFT\",\"OPENWAY_NEW"]}

There are two backslashes which brake my request.

I've got the main problem is in the setting "productSet". Postman makes the string in the end. And inside the string it should mark quotes.

What should I change to make a correct request without auto-generated backslahes?

答案1

得分: 0

The split helped

if (pm.collectionVariables.get("sourceSystem") === pm.collectionVariables.get("targetSystem")) {
    pm.collectionVariables.set("productSet", "sourceSystem");
} else {
    pm.collectionVariables.set("productsString", pm.collectionVariables.get("sourceSystem") + "," + pm.collectionVariables.get("targetSystem"));
    splitString = pm.collectionVariables.get("productsString").split(",");
    pm.collectionVariables.set("productSet", splitString);
};

I'm not sure about its beauty or if it's possible to do it better. If you know, please write.

英文:

The split helped

if(pm.collectionVariables.get("sourceSystem")===pm.collectionVariables.get("targetSystem")){
    pm.collectionVariables.set("productSet", "sourceSystem")
}else{
    pm.collectionVariables.set("productsString", pm.collectionVariables.get("sourceSystem") +","+ pm.collectionVariables.get("targetSystem"));
    splitString = pm.collectionVariables.get("productsString").split(",");
    pm.collectionVariables.set("productSet", splitString);
};

I'm not sure about it's beauty or it's possible to do it better.
If you know it, please write.

huangapple
  • 本文由 发表于 2023年7月17日 19:14:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定