# PM Response within a loop

**URL:** <https://community.postman.com/t/pm-response-within-a-loop/594>\
**Category:** Help Hub\
**Tags:** pm, json\
**Created:** [January 3, 2018, 1:57am UTC](https://community.postman.com/t/pm-response-within-a-loop/594 "2018-01-03T01:57:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark.sadegursky](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mark.sadegursky/32/223_2.png) [@mark.sadegursky](https://community.postman.com/u/mark.sadegursky)\
**Post date:** [January 3, 2018, 1:57am UTC](https://community.postman.com/t/pm-response-within-a-loop/594/1 "2018-01-03T01:57:21Z")

</div>

Hi!

I have an initial call to an API that gives me a list, that I then loop through and want to export the response from the 2nd call to the console. However, the console only seems to output the response of the initial call.

Help would be greatly appreciated.

Here is the Test Body

var isJson = false;  
var TriggerEvent = ‘’;  
var i;  
var jsonData = ‘’;

try{  
var dataResponse = JSON.parse(responseBody);  
isJson = true;  
}  
catch(err)  
{  
console.log(‘responseBody is not JSON’);  
}

if(isJson)  
{  
postman.setEnvironmentVariable(“arrayLength”, \_.size(dataResponse.triggeringEvents) );

```
for (i = 0; i < environment.arrayLength; i++)  
{   
    TriggerEvent = '';
    postman.setEnvironmentVariable("TriggerEvent" , dataResponse.triggeringEvents[i]);
    pm.sendRequest({
        url: pm.environment.get("url") + '/api/v1/administrators/marketplaces/' + pm.environment.get("default_marketplace") + '/message_templates?triggeringEvent=' + pm.environment.get("TriggerEvent"), 
        method: 'GET',
        header: 'X-Token:' + pm.environment.get("token"),
        body: {
            mode: 'json'
        }
    },
    console.log(pm.response.json()) //This is giving the response from the the GET not the GET in the pm.sendRequest, how do you get that response outputted. 
    );
}

```

}

---

<div class="post-metadata">

**Author:** ![pratik](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pratik/32/224_2.png) [@pratik](https://community.postman.com/u/pratik)\
**Post date:** [January 3, 2018, 8:59am UTC](https://community.postman.com/t/pm-response-within-a-loop/594/2 "2018-01-03T08:59:04Z")

</div>

You need to use the response available in the callback that you pass to `pm.sendRequest`

```
   pm.sendRequest({
        url: "",
        method: "GET",
        headers: {},
        body: {}
     },
     function (err, res) {
        // This is the response you are looking for
        console.log(res);
     }
    );
```

---

<div class="post-metadata">

**Author:** ![mark.sadegursky](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mark.sadegursky/32/223_2.png) [@mark.sadegursky](https://community.postman.com/u/mark.sadegursky)\
**Post date:** [January 3, 2018, 2:16pm UTC](https://community.postman.com/t/pm-response-within-a-loop/594/3 "2018-01-03T14:16:53Z")

</div>

Thanks for this.  
What I’ve noticed is that when I run this with Newman, that some strings are getting line breaks to the next line. I have some long HTML strings, but don’t want them cut off. Is there a way to prevent this?

---

<div class="post-metadata">

**Author:** ![pratik](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pratik/32/224_2.png) [@pratik](https://community.postman.com/u/pratik)\
**Post date:** [January 3, 2018, 5:35pm UTC](https://community.postman.com/t/pm-response-within-a-loop/594/4 "2018-01-03T17:35:32Z")

</div>

Newman uses the cli reporter by default if no other reporter is specified. The cli reporter prettifies the console log output by using appropriate colors and wrapping the text based on the dimensions of the window the console is running in.

This is what I could figure out by looking at the code in [utils](https://github.com/postmanlabs/newman/blob/develop/lib/reporters/cli/cli-utils.js) and [the code](https://github.com/postmanlabs/newman/blob/develop/lib/reporters/cli/index.js) that listens to the console log events.

You might be able to tweak that code a bit to take in a configuration to not wrap the log and send a PR to that code. Alternatively you could also [create your own custom reporter](https://github.com/postmanlabs/newman#creating-and-using-custom-reporters) or use one of the other reporters available.
