Chaining Requests

Hello,

I’ve looked at several tutorials on the blog and youtube, but I don’t have a development background and not quite sure how to translate their specific use cases to mine. (I tried linking them but my account isn’t old enough)

Ok; now on to what I want to do. I work for a weather forecasting organization and we have thousands of weather stations that are constantly being updated so I have one API that just gives me every single station in our network:

 "id": "037",
"name": "Field 37 (Nord-NW)",
"latitude": 39.81953,
"longitude": -122.01292,
"elevation": 156,
"timeZone": "America/Los_Angeles"

In the next API we use the id to request the latest reading of that station:
api.url.com/stationdata/{interval}/{station}/latest

To do this I have entered the following code in the tests of the first GET request.

var jsonData = JSON.parse(responseBody);
pm.environment.set(“station”, jsonData.id);

Then in the second GET request I have

api.url.com/stationdata/{interval}/{{station}}/latest

But postman says the variable is undefined and in the environment tab it doesn’t provide any initial or current values. The point behind all of this is to get the data into Microsoft’s Power BI so I can create graphs and maps and such; but don’t want to have to manually bring in the current API every time we add a new station (which is basically daily).

Thanks for any help you can provide

Hey @ccowin

Is that the full response coming back from your request?

If that response body you posted is an object in an array then that might be the reason why it’s undefined.

[
     {
         "id": "037",
         "name": "Field 37 (Nord-NW)",
         "latitude": 39.81953,
         "longitude": -122.01292,
         "elevation": 156,
         "timeZone": "America/Los_Angeles"
     }
]

It repeats in that pattern for like 12,000 lines

Is there a way for me to determine if it is an array?

edit: Ah I see that the square bracket means it is in an array. Is there a way around this?

OK.

So that’s an array or a list with a bunch objects, basically each group of properties inside the { }. That’s zero indexed and to make a reference to a particular part of the list, you will need to supply a number in [ ].

To get to the data in the first object you will need to make a reference to it like jsonData[0].id. As it’s zero indexed, the 0 is the first position.

To get each of the id values, you will need to iterate through the objects.

OK, that makes sense.

Is the iteration done via the iterations box in the Collection Runner or is it some code? I did a quick search and couldn’t find anything other than the box. If it is the Collection Runner, I assume i need to make it run for however many stations I have, do I need to do anything to make it skip the ones it already has?

So by iterate through the objects, I meant in the sense of writing some JS code in the Tests tab, to get all the id values from each of the objects in the response.

I mocked out a sample of your data to hopefully help explain this more:

[
    {
        "id": "037",
        "name": "Field 37 (Nord-NW)",
        "latitude": 39.81953,
        "longitude": -122.01292,
        "elevation": 156,
        "timeZone": "America/Los_Angeles"
    },
    {
        "id": "038",
        "name": "Field 38 (Nord-NW)",
        "latitude": 39.8155,
        "longitude": -152.76862,
        "elevation": 201,
        "timeZone": "America/Los_Angeles"
    }
]

By adding something like this code to the Tests tab, it will iterate through the objects in the response and extract all the id values and then place them in a list for use in a future request. It’s using the _.each() function from a built-in module called Lodash.

let idList = []
_.each(pm.response.json(), (arrItem) => {
    idList.push(arrItem.id)
})

pm.globals.set('idList', JSON.stringify(idList))

If you wanted to skip past ones that it already knows about you would need to add some logic around that or add something to keep track of the last id used etc.

You mentioned the Collection runner iterations, that is something different - You can find more information about that here:

https://learning.getpostman.com/docs/postman/collection_runs/running_multiple_iterations

1 Like

Danny,
Sorry for the delay, had to go on a quick trip.

This works great, now I think the only issue is that it is placing the quote marks around each value into the url string of the next request which it can’t read.

I actually found this stack overflow thread where you responded to about a month ago as well, which I found quite funny.

Ha! :trophy:

Pretty much the same answer…at least I’m consistent :slight_smile: