Save multiple responses to different variables

Hi all, hope you as well.

My question is, I need to save a value from a response in a variable (this step is ok), but the problem is, we are using here a CSV input for this request on the collection runner.

image

When I do that I have a lot of responses (one for each line in the CSV input) using a single request.

Which type of β€œTest” do I need to use to create a variable for each response for the CSV line entry? the way we know here we create an environment variable with a specific name, but if I run the inputs all the values I need in the response will go to this same variable and will replace the last value.

This is how we save the value in a variable here:
image

Is it possible to put a counter in the variable name or something like that to have different names for each CSV entry and save all the responses?

Thanks a lot,

Hey @gabriel

You could have something simple that using the pm.info.iteration function to add the current iteration to the variable name? I’ve added a +1 as the number is zero indexed.

pm.environment.set(`response-${pm.info.iteration + 1}`, JSON.stringify(pm.response.json()))
3 Likes

Thank you so much, it worked!

Just one more doubt is it possible to save just a specific value from the response in that way? Just to be faster.

Thanks,
Gabriel

1 Like

You can save whatever you like :smiley:

That was just a generic solution as I don’t know the full context and response structure of your API. :smiley:

1 Like

Thank you so much Danny. You helped me alot

1 Like

This has been extremely helpful so thank you, however, I have a new problem. I have saved values in this way, in my case I have 48 ids set in the environment as id1-id48. In a separate collection I want to grab these ids in order 1-48 and place them in 48 get requests to test the json schema of the async responses. I have my other values for the request stored in a csv which I use to run my collection. How would I go about doing this?