# Trying to load variables from the file

**URL:** <https://community.postman.com/t/trying-to-load-variables-from-the-file/17769>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [November 17, 2020, 10:34am UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769 "2020-11-17T10:34:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vohratom](https://avatars.discourse-cdn.com/v4/letter/v/e19b73/32.png) [@vohratom](https://community.postman.com/u/vohratom)\
**Post date:** [November 17, 2020, 10:34am UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/1 "2020-11-17T10:34:12Z")

</div>

Hey there, I would love to create a simple check that compares that the city name of the API matches the city name from the file.

**URL**

```auto
GET - api.openweathermap.org/data/2.5/weather?q={{$setCityName}}&appid=304656eaa6952374cdd57a2b6100659b

```

**pre-request Script:**

```auto
pm.test("Set city name", function () {
    var setCityName = pm.iterationData.get("name");
});

```

**Tests:**

```auto
pm.test("Response has set correct data value", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.name).to.equal(pm.iterationData.get("name"));
});

```

**Body:**

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/c/9caa7adc4c390f7392029bf99ffbcea47fdd3099.png)

**test.json file:**

```auto
[
    {
        "id": 3067696,
        "name": "Prague",
        "state": "",
        "country": "CZ",
        "coord": {
            "lon": 14.42076,
            "lat": 50.088039
	}
    },
    {
        "id": 2643743,
        "name": "London",
        "state": "",
        "country": "GB",
        "coord": {
            "lon": -0.12574,
            "lat": 51.50853
        }
    }
]

```

**Result:**

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

what am I doing wrong? 🙂

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 17, 2020, 12:27pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/2 "2020-11-17T12:27:24Z")

</div>

Your `setCityName` variable isn’t set.

You can tell because it is highlighted in red in the request url, but you can also see in the console at the bottom that the variable isn’t resolving.

---

<div class="post-metadata">

**Author:** ![vohratom](https://avatars.discourse-cdn.com/v4/letter/v/e19b73/32.png) [@vohratom](https://community.postman.com/u/vohratom)\
**Post date:** [November 17, 2020, 12:34pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/3 "2020-11-17T12:34:47Z")

</div>

Hi Allen.helton,

yeah I noticed that, thank you for letting me know, would you be so nice and try to let me know how can I set the variable to be the value of the JSON file in the “name”?

I guess there are at least two steps:

1. attaching the file probably in the Body \> form-data tab
2. setting the variable setCityName to that file… probably by var setCityName = pm.iterationData.get(“name”); or sth like that…

Thank you very much for your response! 🙂

All I need to do is to set variable to load from external file ☹

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 17, 2020, 12:44pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/4 "2020-11-17T12:44:33Z")

</div>

If you’re just using it in the url like that, you don’t need to do anything special. Just change the variable in the url to `{{name}}` since that is how it’s defined in your json data

---

<div class="post-metadata">

**Author:** ![vohratom](https://avatars.discourse-cdn.com/v4/letter/v/e19b73/32.png) [@vohratom](https://community.postman.com/u/vohratom)\
**Post date:** [November 17, 2020, 2:02pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/5 "2020-11-17T14:02:38Z")

</div>

It still does not work ☹

I tried to create a variable {{city}}

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/f/1f09cb05968437c68e4fd974a9582bab1c2a80bb.png)  
but when I send a request it considers {{city}} to be a “City” and shows the result of the city called City - [http://api.openweathermap.org/data/2.5/weather?q=city&appid=304656eaa6952374cdd57a2b6100659b](http://api.openweathermap.org/data/2.5/weather?q=city&appid=304656eaa6952374cdd57a2b6100659b)

But there is no way how to change {{city}} variable to anything else to be working in the URL request

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/c/5c1355f7a0298a3c19835125700f91746cda5cbc.png)

I also tried to set the file to the Body \> raw section but I do not know how to access that data…

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/8/a874b89253d2d8f771ea1c7cf8dfaf15711e5249.png)

Any ideas? ☹ I am kinda stuck here for couple hours 😃 it works fine when I manually enter **?q=prague** but I want it to be loaded from the file…

Everything in pre-request Script is commented out…

Thank you

---

<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:** [November 17, 2020, 2:18pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/6 "2020-11-17T14:18:20Z")

</div>

I see you have alot of unsaved request tabs, those changes will not be picked up by the Collection Runner, until you save the requests.

---

<div class="post-metadata">

**Author:** ![vohratom](https://avatars.discourse-cdn.com/v4/letter/v/e19b73/32.png) [@vohratom](https://community.postman.com/u/vohratom)\
**Post date:** [November 17, 2020, 2:40pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/7 "2020-11-17T14:40:59Z")

</div>

Hi DannyDainton,

thank you for your reply

I have saved and closed everything.

Now the request looks like this:  
**Params**

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

**Body**

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

**Pre-request Script**  
**commented:**

```auto
    /*
pm.test("Set city name", function () {
    var setCityName = pm.iterationData.get("name");
    console.log(pm.iterationData.get("name"));
});
*/

```

**Tests**

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

I am running it not in the Collection but I am hitting ctrl+enter to see if it is working or not…

Could anyone be so nice and try to enter the working piece of code? 😍

Thank you!

---

<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:** [November 17, 2020, 3:51pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/8 "2020-11-17T15:51:18Z")

</div>

It’s not resolved as anything though - Why would that work?

Datafile’s and the `pm.iterationData.get()` function will _only_ work with the Collection Runner and not in the individual requests.

It’s going to work in a single request, if you have that `setCityName` variable, with a value, set at the Collection, Environment or Global level.

A local variable which would not be stored but run with the request would also work if it were added to the `Pre-request`:

```auto
pm.variables.set("setCityName", "Prague")

```

For example, this value only lives for the lifecycle of this request:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/4/a470df038ca9a4af7976aa35bd487416e5f305df.png)

---

<div class="post-metadata">

**Author:** ![vohratom](https://avatars.discourse-cdn.com/v4/letter/v/e19b73/32.png) [@vohratom](https://community.postman.com/u/vohratom)\
**Post date:** [November 17, 2020, 6:46pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/9 "2020-11-17T18:46:21Z")

</div>

Hi DannyDaiton,

thank you very much for your help I did figure it out based on your suggestions! 🙂

For others who will be struggling with sth similar:  
**Pre-request Script**

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/8/b899dceacaff47799569ed5969544a5dadabe7e9.png)

**Tests**

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

**Body**

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

Thank all of you one more time 🙂

---

<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:** [November 17, 2020, 6:59pm UTC](https://community.postman.com/t/trying-to-load-variables-from-the-file/17769/10 "2020-11-17T18:59:05Z")

</div>

It’s still unsure what it is you’re trying to do.

The test in the pre-request isn’t really doing anything and the code within it doesn’t need to be there. It will work outside of that test function.

Does that endpoint expect a file in a GET request? Not sure what’s happening there.

You’re still using `iterationData` in the tests but that won’t do anything unless you’re using the Collection Runner and you’re using a datafile on the Collection Run.

Does that test pass when you make that request?
