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?
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.