# Reading values from the "Body" and setting environment variables

**URL:** <https://community.postman.com/t/reading-values-from-the-body-and-setting-environment-variables/6802>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [July 26, 2019, 5:05pm UTC](https://community.postman.com/t/reading-values-from-the-body-and-setting-environment-variables/6802 "2019-07-26T17:05:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jmeldrum](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jmeldrum](https://community.postman.com/u/Jmeldrum)\
**Post date:** [July 26, 2019, 5:05pm UTC](https://community.postman.com/t/reading-values-from-the-body-and-setting-environment-variables/6802/1 "2019-07-26T17:05:15Z")

</div>

Hi,

I am trying to ready values from the BODY and a json request (no the response) and then set it in a variable. I can successfully do it the response body but not the request body.

Its a post request

**Body:**

```
{
  "PolicyDN": "\\VED\\Policy\\Demo\\APIs\\Certificates",
  "ObjectName": "api-example.se.venafi.com",
  "Subject": "api-example.se.venafi.com",
  "SubjectAltNames": [
        {
      "TypeName": "DNS",
      "Name": "venafisan-example.se.venafi.com"
        },
        {
      "TypeName": "DNS",
      "Name": "venafisan-example2.se.venafi.com"
        }
    ]
}

```

**Tests Tab**

tests[""] = !(responseBody.has(“Error”));  
var data = JSON.parse(responseBody);  
postman.setEnvironmentVariable(“Cert\_DN”, data.CertificateDN.replace(/\/g,"\$&"));  
postman.setEnvironmentVariable(“GUID”, data.Guid);

**Tyring to add something for the "Request Value in the request body. Something like this (This does not work)**

var mydata = JSON.parse(requestBody);  
postman.setEnvironmentVariable(“Subject”, mydata.Subject);

I am sure the solution/syntax is easy. Just not sure how to do it.

Any help would be greatly appreciated.

---

<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:** [July 26, 2019, 6:51pm UTC](https://community.postman.com/t/reading-values-from-the-body-and-setting-environment-variables/6802/2 "2019-07-26T18:51:06Z")

</div>

Hey @Jmeldrum

Are you trying to set a variable from the request body? I’m unsure of the reason for doing this.

Would you be able to explain your use case a little bit more to help provide context and allow us to get to a solution quicker?

---

<div class="post-metadata">

**Author:** ![kishordige](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kishordige](https://community.postman.com/u/kishordige)\
**Post date:** [March 24, 2023, 2:38pm UTC](https://community.postman.com/t/reading-values-from-the-body-and-setting-environment-variables/6802/3 "2023-03-24T14:38:26Z")

</div>

In the test script, you can parse request body by the following way:

```javascript
let requestData = JSON.parse(pm.request.body);
pm.globals.set("username", requestData.username);

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 22, 2023, 12:01pm UTC](https://community.postman.com/t/reading-values-from-the-body-and-setting-environment-variables/6802/4 "2023-08-22T12:01:43Z")

</div>

Closed on August 22, 2023, 12:01pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
