Need help with this Variable request chain

Hi,

So I have 2 questions with Chaining API calls. My first call is to a list of hotels and some basic data. In this call I can just get basic information about the hotel. When I call it currently I am only using it to get the hotelIDs to then pass those to another call that will have all of the data on the hotels. Within that first call I then store the Hotel ID in a variable using this post response script.

// save response from the response body so we can access this data in this scripting area
let response = pm.response.json();
// create a new array of just the breeds returned in the response body
let responsehotelIds = response.result.data.map(hotel => hotel.id);
console.log("hotelIdsFromVirtuoso", responsehotelIds);
// save this information as an environment variable, so we can access this info in subsequent requests
// remember to encode and serialize this data properly
pm.environment.set("hotelIds", JSON.stringify(responsehotelIds));

This will then store all of the hotel ID’s (over 1900 of them) in a variable for me to use.

I then have 2 issues with the next API call:

  1. I want to then call on each of the hotel IDS 1 by 1 then need to be placed in a query one by one
  2. With each call I want to then put a certain amount of that data into a row in a excel spreadsheet or csv and then move on to the next hotelID and keep adding to the table to then be able to export the entire list as a spreadsheet or csv. Any thoughts on how I would achieve this?

Thanks
Scott

You need the two requests in your collection.

Your initial request to get all of the hotels with the code that you currently have.

The end result should be an environment variable with an array of hotels ID’s.

On your second request, you will need to retrieve this variable in the pre-request script. (Remember to JSON parse when retrieving the variable).

You will need to use the Postman setNextRequest function to keep looping on the same request while the array is larger than zero.

To do this, you will use a method available to arrays called shift() which will take the first element of the array, and then delete it from the array.

Part 2 is not possible directly in the Postman sandbox as you have very limited access to the local file system. You can either send the data after the loop finishes to a third party system like SharePoint via another API, or you can use the Postman node.JS libraries (which will mean you need to have a good knowledge of JavaScript and node).

Have a look here for a working example of using setNextRequest that you can tailor to your requirements.

setNextRequest example does not work - Ask the Experts and Postman Tips - Postman Community

My Hotel Info API

Hi mike this makes a bunch of sense. I can obviously figure out the second part after, probably makes sense to send the info to another service like Zapier to then put the data into a table. Could you take a look at the project and see if I am doing this right. I am newer to postman and API in general and I think I am getting some steps wrong.

I think I am close but off a little

Thanks,
Scott

I’ve requested access to that resource.