Need to capture response body of each iteration

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

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

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.

1 Like

@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.

@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:

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

Like so:

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

1 Like

@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.

1 Like

@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?

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):

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

i have 3 request in collection and i need to write all three request in one csv file
like image

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

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.

@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)

@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:

// 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);
});

@singhsivcan beautiful. thank you so much

1 Like

@singhsivcan - when I am running this code getting POST 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.

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