# How to validate an environment variable in a response of get request

**URL:** <https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142>\
**Category:** Help Hub\
**Created:** [August 13, 2019, 8:08pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142 "2019-08-13T20:08:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![NitiJabin](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@NitiJabin](https://community.postman.com/u/NitiJabin)\
**Post date:** [August 13, 2019, 8:08pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/1 "2019-08-13T20:08:41Z")

</div>

I have set an environment variable from a response of Post request and variable has set well. now I want to verify whether that value is saved in database by pulling the data using Get request.  
I used below code under tests of the Get request.  
tests[“Body matches string”] = responseBody.has(promo);

but it is not able to get the promo and showing the below error message

“ReferenceError | promo is not defined”

---

<div class="post-metadata">

**Author:** ![lvaltierra](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lvaltierra](https://community.postman.com/u/lvaltierra)\
**Post date:** [August 13, 2019, 8:43pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/2 "2019-08-13T20:43:46Z")

</div>

Excuse me if I did not understand your question correctly, but you basically want to compare your variable from the POST against what is returned from the response? If that’s the case, assuming the response is json, you can do something like the following…

pm.test(“name of your test”, function () {  
const jsonData = pm.response.json()  
pm.expect(jsonData.{{property}}).to.eql(pm.environment.get({{variable}}))  
});

---

<div class="post-metadata">

**Author:** ![NitiJabin](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@NitiJabin](https://community.postman.com/u/NitiJabin)\
**Post date:** [August 13, 2019, 10:57pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/3 "2019-08-13T22:57:51Z")

</div>

Hi, thank you for the reply.  
But my problem is in the pm.environment.get({{variable}}). Environment variable “promo” is not getting and showing error in test result.

 ![37%20PM](https://us1.discourse-cdn.com/getpostman/original/2X/b/b6d3d20f58ba58287860fbebbdee63398b9300e5.png)

 ![57%20PM](https://us1.discourse-cdn.com/getpostman/original/3X/5/a/5aa7cab2abb219281fa6c3d50fa88e5feed7d071.png)

---

<div class="post-metadata">

**Author:** ![lvaltierra](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lvaltierra](https://community.postman.com/u/lvaltierra)\
**Post date:** [August 13, 2019, 11:01pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/4 "2019-08-13T23:01:11Z")

</div>

When calling pm.environment.get({promo}) try quotation marks instead of the curly braces.

pm.environment.get(“promo”)

---

<div class="post-metadata">

**Author:** ![NitiJabin](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@NitiJabin](https://community.postman.com/u/NitiJabin)\
**Post date:** [August 13, 2019, 11:17pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/5 "2019-08-13T23:17:30Z")

</div>

It works. Thank you.

---

<div class="post-metadata">

**Author:** ![tvaroglu](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@tvaroglu](https://community.postman.com/u/tvaroglu)\
**Post date:** [January 21, 2020, 10:01pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/6 "2020-01-21T22:01:40Z")

</div>

This worked! Thank you. Have been searching for this for awhile now, really appreciate your help here.

---

<div class="post-metadata">

**Author:** ![parasverma](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@parasverma](https://community.postman.com/u/parasverma)\
**Post date:** [May 5, 2021, 5:22pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/7 "2021-05-05T17:22:38Z")

</div>

What if the variable value type is Number if we run this test case it gives quotes to the value which doesn’t match the response

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/2/12332588eb82bb1848b443193ed4e43db418d808.png)

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [May 5, 2021, 5:47pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/8 "2021-05-05T17:47:09Z")

</div>

Hi @parasverma,

That’s because when you store a value in a variable, it automatically gets stored as a string, regardless of its original type. You’ll need to do a simple type conversion back, so that they have identical types again, like this:

```
pm.test("lat is matching", function () {
    var jsonData = pm.response.json();
    var expectedLat = parseFloat(pm.environment.get("lat"));
    pm.expect(jsonData.data.lat).to.eql(expectedLat);
});
```

---

<div class="post-metadata">

**Author:** ![parasverma](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@parasverma](https://community.postman.com/u/parasverma)\
**Post date:** [May 5, 2021, 6:02pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/9 "2021-05-05T18:02:50Z")

</div>

Thanks @neilstudd

This is better and meanwhile I used another way below but what you told is better coz as it is under the function.

var longitutde = pm.environment.get(“long”);  
longitutde = longitutde.toNumber();  
pm.test(“lng is matching”, function () {  
var jsonData = pm.response.json();  
pm.expect(jsonData.data.lastSuccessLocation.geoAddress.coordinates.lng).to.eql(longitutde);  
});

---

<div class="post-metadata">

**Author:** ![parasverma](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@parasverma](https://community.postman.com/u/parasverma)\
**Post date:** [May 5, 2021, 6:13pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/10 "2021-05-05T18:13:52Z")

</div>

Hi @neilstudd

Can you please also help me with how to validate Date and time, please correct me what I am missing

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/d/6d81bcbab2de51b8ca454a9b93af10505c7bbc05.png)

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [May 5, 2021, 6:24pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/11 "2021-05-05T18:24:42Z")

</div>

@parasverma In this case, the values actually are different! The dates are the same, but the time that you retrieved from the environment variable is 5½ hours earlier. It seems that the environment variable may be saved in UTC time.

Are you able to share the code which is setting the `LocationUpdateTime` environment variable? Assuming you want the environment variable to contain the same value that you are now retrieving, you’ll need to make some changes to that code.

---

<div class="post-metadata">

**Author:** ![parasverma](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@parasverma](https://community.postman.com/u/parasverma)\
**Post date:** [May 5, 2021, 6:48pm UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/12 "2021-05-05T18:48:59Z")

</div>

Yes code was changing the time format actually thanks for your help @neilstudd  
Thanks for your quick responses.

---

<div class="post-metadata">

**Author:** ![TechnocratsAdmin](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/technocratsadmin/32/11293_2.png) [@TechnocratsAdmin](https://community.postman.com/u/TechnocratsAdmin)\
**Post date:** [August 24, 2021, 11:02am UTC](https://community.postman.com/t/how-to-validate-an-environment-variable-in-a-response-of-get-request/7142/13 "2021-08-24T11:02:57Z")

</div>

Hello  
I have created a video to explain how to assert response in postman [Postman API Test Cases Script tutorial with examples and tips to solve common errors - YouTube](https://youtu.be/QH9K5MZ_ABE)  
Hope it helps
