英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论