MoNa
MoNa

Reputation: 471

How to use conditional statement in Postman Body section

I have a scenario to validate multiple datasets with the same logic. Only change is dataset location.

Eg:

Apple, Sony, MI are the datasets, their data is placed in separate folder. If I pass the variable name as Apple, then in postman body part it should check which data to pass.

Body Section in Postman using JSON

   {
    "if" : { {{Mobile}} : "Apple"},
    "then" : {"location"},
    "else":
    {
     "if" : { {{Mobile}} : "Sony"},
     "then" : {"location"}
    }
  }

If I use the above code, I got the response as Undefined and 200 OK

My expected response should be some ID value (eg: 1,2,3 etc.)

Upvotes: 0

Views: 2227

Answers (1)

PDHide
PDHide

Reputation: 19949

change variable from pre-request, set body as :

 {
    "mobile":"{{mobile}}"
    "location":"{{location}}"
  }

and in pre-request

const mobile = pm.variables.get("mobile")
if(mobile ==="Apple"){
pm.variables.set("location","somelocation1")
}else if ( mobile === "sony"){

pm.variables.set("location","somelocation2")

}

Upvotes: 1

Related Questions