How to send a request multiple times with different data?

Hello everyone!

I want to have a collection which will contain a request. I want that request to be sent 3 times when running with Postman runner and each time with different data.
This is my example, but when I run with runner it only posts the first data , the other two are not posted.

Request - Method: POST

{{url}}/album/items

Body:

{ "item_id": "{{clipart_id}}", "item_type": "clipart" }

Pre-Request Scripts

    var clipArtIds = pm.environment.get("clipArtIds");

        clipArtIds = ["320778942558211", "320825615368211", "320768867352211"]


    var currentClipArtId = clipArtIds.shift();
    pm.environment.set("clipArt_id", currentClipArtId);
    pm.environment.set("clipArtIds", clipArtIds);

Tests

var clipArtIds = pm.environment.get("clipArtIds");

if (clipArtIds && clipArtIds.length > 0) {
    postman.setNextRequest("{{url}}/albums/items");
} else {
    postman.setNextRequest(null);
}

So, when sending just one request that 3 items should be posted in the album after that It should stop in order to run the next separated request in the collection.
How can I solve this ?

Hi @anna.tirshenko!

What jumps out at me is you’re getting clipArtIds from the environment, but then you’re overwriting that with a different array.

var clipArtIds = pm.environment.get("clipArtIds");

// remove the line below
        clipArtIds = ["320778942558211", "320825615368211", "320768867352211"]

What if you remove the second line in the pre-request script? Does it work?

Hi @anna.tirshenko,

You may want to look at using JSON.stringify(clipArtIds) when storing the array and JSON.parse(clipArtIds) upon retrieval.

Also, it looks like there might be a typo in your request body syntax. You’re storing the individual ID as clipArt_id, not clipart_id.

Hope that helps!

Hi!
I am trying to implement the same logic: I have a Post request and a list of different parameter values to be send. But it isn’t working, only first request is being sent and postman.setNextRequest doesn’t send anything at all.
The code schema is pretty simple for such case but no idea why it is not working.
Please, give more fresh ideas!

POST {{url}}/locale/setRegion

Body

{
    "params": {
        "country": "russia",
        "city": "{{currentCity}}"
    }
}

Pre-request Script

var cities = pm.environment.get('cities');
    if (!cities) {
        cities = ['moscow', 'saint_petersburg', 'kazan', 'rostov_on_don',

                       'nizhny_novgorod', 'krasnodar', 'sochi', 'simferopol', 'samara',

                       'novosibirsk', 'kaliningrad', 'ekaterinburg', 'ufa', 'voronezh', 'chelyabinsk', 'krasnoyarsk', 'perm', 'kursk', 'omsk', 'barnaul']
    }

    var currentCity = cities.shift();
    pm.environment.set('currentCity', currentCity);
    pm.environment.set('russianCities', cities);

Tests

var cities = pm.environment.get("russianCities");

if (cities && cities.length > 0){
      postman.setNextRequest("{{url}}/locale/setRegion");
}else{   
      postman.setNextRequest(null);
}

Hey @simakanja

Welcome to the community! :wave:

Can you share what you have so far, it will make it easier for people to suggest possible solutions.

Hi @dannydainton ! Thanks! :upside_down_face:
Updated my above message with code.

Is “{{url}}/locale/setRegion” the name of the request?

Would this get you past sending the first request?

postman.setNextRequest(pm.info.requestName);

Pardon, not quite right code
in Tests I grab the {{url}} from the environment variables and concatenate like this:

var url = pm.environment.get("my_url")
postman.setNextRequest(url + "/locale/setRegion");

And this url equals to the url in the very first request.

Is that the name of the request?

I understand that it’s going to a specific URL but the sendNextRequest should equal a request name string, rather than a URL location.

@dannydainton, Got it, but I also tried it in vain: putting the full request string to pm.setNextRequest(). No effect.

I can’t see what you can see so you might need to show an image of this. :grin:

Ensure that after making any change to the request, you’re saving the change as the runner wouldn’t know about this until you do that.

@dannydainton Okay, I’ll paste screenshots here. =) By the way I do save my script before running

What’s the output of the Collection Runner?

@dannydainton Here, only one request.

The request is called set_russian_city that’s what needs to be in the postman.setNextRequest() function.

postman.setNextRequest("set_russian_city")

Or this to add that without hardcoding the string.

postman.setNextRequest(pm.info.requestName)
1 Like

@dannydainton Oh, my Goodness, it works.
Big thanks for your help!