# Need to capture response body of each iteration

**URL:** <https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657>\
**Category:** Help Hub\
**Created:** [December 24, 2018, 3:30pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657 "2018-12-24T15:30:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartsurender91](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@smartsurender91](https://community.postman.com/u/smartsurender91)\
**Post date:** [December 24, 2018, 3:30pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/1 "2018-12-24T15:30:39Z")

</div>

Hello All,  
I am created and API request in Postman and i am iterating the same for 30 iterations with different values such as passing different username everytime.  
Now i have kept certain test in test scripts.  
now in result i want to save request/response created/ downloaded in each iteration .  
do we have any solution for that.

basically i want to create a report like postman does when i execute test using runner.  
but i am unable to export the exact report from generated by runner in HTML or some other format.

any suggestions will be really help me.  
Thanks in anticipation

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [December 26, 2018, 10:08am UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/2 "2018-12-26T10:08:30Z")

</div>

This collection might help you in writing the response data to a file:

> **[Write Responses To File](https://documenter.getpostman.com/view/3407886/RWgp1fB5)**
>
> This collection makes it easy for users who want to write the response of each request to a file.
> This can be extended to write anything for eg. meta information or value of variables being used.
> 
> 
> This project is using a powerful feature built in...

You can take the idea from it on how you can use `pm.sendRequest` feature of postman and build upon the nodejs server code and the script to make your own reporter etc.

---

<div class="post-metadata">

**Author:** ![rpearcy](https://avatars.discourse-cdn.com/v4/letter/r/e274bd/32.png) [@rpearcy](https://community.postman.com/u/rpearcy)\
**Post date:** [March 12, 2020, 1:31pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/3 "2020-03-12T13:31:52Z")

</div>

@sivcan This is great. Very much appreciated. Is there a way to write results from multiple iterations? For instance, if I use collection runner to run a request 100 times, I’m currently only getting one response and not all 100 written to the file.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [March 12, 2020, 1:51pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/4 "2020-03-12T13:51:10Z")

</div>

@rpearcy Hi, this is because the name of the request is the same and thus, the new data is overriding the existing data in the same file.

What you can do is Right click the collection \>`Edit` \> `Tests`

And change the request name to the following:

```javascript
requestName: `${request.name || request.url}-${pm.info.iteration}`,

```

Like so:

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/a/abae4f277156b424ede808fd65d8279eacb6e340.png)

Now, for every iteration a new file will be stored along with the corresponding iteration that you ran via the runner.

---

<div class="post-metadata">

**Author:** ![rpearcy](https://avatars.discourse-cdn.com/v4/letter/r/e274bd/32.png) [@rpearcy](https://community.postman.com/u/rpearcy)\
**Post date:** [March 12, 2020, 3:26pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/5 "2020-03-12T15:26:28Z")

</div>

@singhsivcan Thanks for the response. I ended up doing something similar by adding Date.now() to the file name so it will give me a unique file name for each iteration.

Thanks again for this. Helped me quite a bit to accomplish some testing at work.

---

<div class="post-metadata">

**Author:** ![dbraus](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@dbraus](https://community.postman.com/u/dbraus)\
**Post date:** [March 30, 2020, 4:39pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/6 "2020-03-30T16:39:01Z")

</div>

@singhsivcan,

I am using the Collection runner and pulling the request body in from an external file. Any idea how I could pull the name of that external file into the name of the file created for the response body?

---

<div class="post-metadata">

**Author:** ![dbraus](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@dbraus](https://community.postman.com/u/dbraus)\
**Post date:** [April 8, 2020, 6:00pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/7 "2020-04-08T18:00:31Z")

</div>

Turns out the name of the external file used to populate the request body cannot be grabbed. My workaround was to use a unique identifier in the request body (also had to scrub this string for invalid filename characters):

 ![Capture](https://us1.discourse-cdn.com/getpostman/original/3X/8/e/8e0d74a4e04c2c790bb338e05b03d2fe428b0c57.jpeg)

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 16, 2020, 4:50pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/8 "2020-09-16T16:50:24Z")

</div>

can i use to save dynamic itemid in Json response, instead of whole body ? with multiple iteration ?

---

<div class="post-metadata">

**Author:** ![innowerademo](https://avatars.discourse-cdn.com/v4/letter/i/85f322/32.png) [@innowerademo](https://community.postman.com/u/innowerademo)\
**Post date:** [November 26, 2020, 10:06am UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/9 "2020-11-26T10:06:04Z")

</div>

i have 3 request in collection and i need to write all three request in one csv file  
like ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/1/81979502f56022379048a38660e780c7f7a554a1.png)

actulay i want to store id and names from every request to use it in another collection

---

<div class="post-metadata">

**Author:** ![BryanCoxCharter](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@BryanCoxCharter](https://community.postman.com/u/BryanCoxCharter)\
**Post date:** [January 27, 2021, 8:57pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/10 "2021-01-27T20:57:04Z")

</div>

This is all good, but the question that I have is: Where is the info being written to? directory? How to find it on a mac.

---

<div class="post-metadata">

**Author:** ![alxpeace](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alxpeace](https://community.postman.com/u/alxpeace)\
**Post date:** [March 4, 2021, 1:09am UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/11 "2021-03-04T01:09:55Z")

</div>

@singhsivcan

I am very happy to have found this. Ive read the docu and setup local server. However when sending request via postman, local server is throwing error:

TypeError [ERR\_INVALID\_ARG\_TYPE]: The “data” argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

any ideas? thank you so much (this will be a massive help to my project)

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [March 4, 2021, 12:09pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/12 "2021-03-04T12:09:00Z")

</div>

@alxpeace Try using the following script in the collection instead of the existing one.

To update the script go to the “Write to response” collection \> Click on the three dots \> “Edit” \> Tests tab.  
Replace the tab with the following script:

```javascript
// The opts for the server, also includes the data to be written to file
let opts = {
    requestName: request.name || request.url,
    fileExtension: 'json',
    mode: 'writeFile', // Change this to any function of the fs library of node to use it.
    uniqueIdentifier: false,
    responseData: pm.response.text()
};

pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(opts)
    }
}, function (err, res) {
    console.log(res);
});

```

---

<div class="post-metadata">

**Author:** ![alxpeace](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alxpeace](https://community.postman.com/u/alxpeace)\
**Post date:** [March 4, 2021, 8:20pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/13 "2021-03-04T20:20:20Z")

</div>

@singhsivcan beautiful. thank you so much

---

<div class="post-metadata">

**Author:** ![Hema](https://avatars.discourse-cdn.com/v4/letter/h/258eb7/32.png) [@Hema](https://community.postman.com/u/Hema)\
**Post date:** [September 7, 2021, 8:28pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/14 "2021-09-07T20:28:04Z")

</div>

@singhsivcan - when I am running this code getting POST [http://localhost:3000/write](http://localhost:3000/write)  
**405** response code

and also can you please help: Where is the info being written to? directory? How to find it on windows.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 23, 2023, 3:46pm UTC](https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/15 "2023-08-23T15:46:26Z")

</div>

Closed on August 23, 2023, 3:46pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
