# Add request body in pre-request script based on the Environmental values

**URL:** <https://community.postman.com/t/add-request-body-in-pre-request-script-based-on-the-environmental-values/14971>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [August 12, 2020, 4:02pm UTC](https://community.postman.com/t/add-request-body-in-pre-request-script-based-on-the-environmental-values/14971 "2020-08-12T16:02:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apalani2](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@apalani2](https://community.postman.com/u/apalani2)\
**Post date:** [August 12, 2020, 4:02pm UTC](https://community.postman.com/t/add-request-body-in-pre-request-script-based-on-the-environmental-values/14971/1 "2020-08-12T16:02:01Z")

</div>

Based on the Environment variable(conditionally) Option A, B, or C, I have to take a different request. How to achieve that?

option A:

```auto
{
    "profile": {
        "firstname": "firstname",
        "lastname": "lastname",
	    **"mobile" : "{{mobile}}"**
    }
}

```

option c:

```auto
{
    "profile": {
        "firstname": "firstname",
        "lastname": "lastname",
        **"email": "{{email}}",**
       **"mobile" : "{{mobile}}"**
    }
}

```

Option B:

```auto
{
    "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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 12, 2020, 4:48pm UTC](https://community.postman.com/t/add-request-body-in-pre-request-script-based-on-the-environmental-values/14971/2 "2020-08-12T16:48:45Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![apalani2](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@apalani2](https://community.postman.com/u/apalani2)\
**Post date:** [August 12, 2020, 4:55pm UTC](https://community.postman.com/t/add-request-body-in-pre-request-script-based-on-the-environmental-values/14971/3 "2020-08-12T16:55:05Z")

</div>

**This is what i have it for pre-request:**

```auto
 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:**

```auto
{{savedData}}

```

**Environmental variable:**

```auto
testData: A

```

The above piece of code works. As an expert advice was it right or what is the alternative way?
