apalani2
(apalani2)
1
Based on the Environment variable(conditionally) Option A, B, or C, I have to take a different request. How to achieve that?
option A:
{
"profile": {
"firstname": "firstname",
"lastname": "lastname",
**"mobile" : "{{mobile}}"**
}
}
option c:
{
"profile": {
"firstname": "firstname",
"lastname": "lastname",
**"email": "{{email}}",**
**"mobile" : "{{mobile}}"**
}
}
Option B:
{
"profile": {
"firstname": "firstname",
"lastname": "lastname",
**"email": "{{email}}"**
}
}
Note: Just the request body is dynamic based on the Environment variable. Hader/URL/method remains the same.
Appreciated your help
What are the conditions that need to be met, which decide what goes into the request body?
Just as a rough example with a basic condition:
let reqBody = {
"profile": {
"firstname": "firstname",
"lastname": "lastname"
}
}
let num = 1;
if(num === 1) {
reqBody.profile.mobile = "{{email}}"
} else {
reqBody.profile.email = "{{email}}"
reqBody.profile.mobile = "{{mobile}}"
}
pm.variables.set("reqBody", JSON.stringify(reqBody))
Then add the {{reqBody}}
variable in the Body tab.
apalani2
(apalani2)
3
This is what i have it for pre-request:
var testData = pm.environment.get("testData");
if (testData === 'A') {
let savedData = JSON.stringify({"iamProfile":{"firstname": "testfirst","lastname":"testlast","autoGeneratedFlag":true,"pin": "{{encryptedText}}","iamemail":"{{emailParam}}"}});
pm.environment.set("savedData", savedData);
// postman.setNextRequest(null);
} else
if (testData === 'B') {
let savedData = JSON.stringify({"iamProfile":{"firstname": "testfirst","lastname":"testlast","autoGeneratedFlag":true,"pin": "{{encryptedText}}","msisdn":"{{msisdn}}"}});
pm.environment.set("savedData", savedData);
// postman.setNextRequest("Second Request");
} else
{
let savedData = JSON.stringify({"iamProfile":{"firstname": "testfirst","lastname":"testlast","autoGeneratedFlag":true,"pin": "{{encryptedText}}"}});
pm.environment.set("savedData", savedData);
}
Body:
{{savedData}}
Environmental variable:
testData: A
The above piece of code works. As an expert advice was it right or what is the alternative way?