Trying to load variables from the file

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

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

pre-request Script:

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

Tests:

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:

test.json file:

[
    {
        "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:

what am I doing wrong? :slight_smile:

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.

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! :slight_smile:

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

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

It still does not work :frowning:

I tried to create a variable {{city}}


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

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

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

Any ideas? :frowning: I am kinda stuck here for couple hours :smiley: 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

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.

Hi DannyDainton,

thank you for your reply

I have saved and closed everything.

Now the request looks like this:
Params

Body

Pre-request Script
commented:

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

Tests

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? :heart_eyes:

Thank you!

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:

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

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

1 Like

Hi DannyDaiton,

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

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

Tests

Body

Thank all of you one more time :slight_smile:

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?