How to export/download Response Body into an external file from Collection Runner Results?

Great! Which route did you go down in the end?

Just to tie this bit off you can change line 23 of script.js to include the Date to make each response a different file and prevent from overwriting the previous query each time.

filePath = `${path.join(folderPath, req.body.requestName)}.${extension}`;

to
filePath = ${path.join(folderPath, req.body.requestName) + Date.now()}.${extension};

To add to this I would like to be able to name the file the name of one of the parameters passed in the api call to be able to distinguish which line in the csv collection file marries up to which response in a more robust way than just top to bottom.

I tried this but I get an error when I run the test:
TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received type undefined

I had a similar issue, it looks like the example provided was missing the “comma” bookends. I fixed it now it’s working.

@singhsivcan
when i run my collection through the runner by passing the input data through the csv file where the iterations are 64. But it ll store only the first iteration response not for all iteration . please suggest where i am wrong .
Thanks in Advance

Actually the script is just overwriting over the same file again and again.
What you can do is modify the name of the request being sent to the local server.

Go to Collection > Edit Collection > Test Script

Change the requestName property to:

requestName: `${request.name || request.url}-${Date.now()}`

Now, it’ll be unique for each request and a new file will be created for each request sent and response received.

2 Likes

Works great! Thanks @singhsivcan!!!
BTW, since I can tell you master this stuff, can you think of a variant to save all the response bodies into the same file with an identifier by other variable? maybe an environment, parameter or variable from the data file?

1 Like

Hi @ Sivcan
It still giving me the single request. I started the node script.js also but write the single response .I have added the function as per your opinion ,Below are the same .

requestName: request.name || request.url + Date.now(),

my OS is windows .

@andres You’d have to edit the node script, you can write a append functionality to the file handling code and while writing to the file, you can add a timestamp along with the request name and then append the entire JSON body to the file.

You can read more about using fs.appendFile functionality in node.

I think you might have incorrectly copied the code that I gave above.

Make sure you’re editing the Collection and not the node script. (You don’t need to).

In the collection put the following again:

requestName: `${request.name || request.url}-${Date.now()}`

Notice the (`) backtick which is helping us achieve string interpolation in javascript.

In case it’s not working, please share your node script + collection script via DM and let’s see!

@singhsivcan
Its working fine now after doing some changes in code . Actually i was not putting the (`) backtick . Finally i solved my problem with your solution .
Great Work @singhsivcan !!!

1 Like

@singhsivcan
I have one APi request which is going to translate the text in to speech . Is any way to download the response in an audio format.
i am able to do it through postman button send and download for single single request .Is any way for a collection of request so that my audio file ll be download ?
Thanks

@singhsivcan Is any way to do the same

i have a csv file which i need to test i want to add the response to the line it just finished iterating any way to do this
thank in advance

Hello Sivcan, I’m just starting to work with postman and I’m having an issue I tried to use your script to save the Response Body into an external file, I have 1 collection with just 1 simple get request, I start the node script.js “ResponsesToFile App is listening now! Send them requests my way!

Data is being stored at location: C:\Users\xxxx\ResponseToFile-Postman\Responses\” but the Response folder is empty.

Do you have any idea? How I could solve this issue? It may seems simple but as I said I’m just starting.

Thank you for your support!!

I’m new to postman as well. And need to store a runner api call response body to a file so i can further work with the data inside the response body.

I’ve installed node.js and installed “express”.

  1. Do I copy the text of script.js into the “pre-request script” or the “tests” tab?
  2. After copying the text of script.js into either tab, I get the error in console “Error: Cannot find module ‘express’”

Thanks for creating this script and posting to the community. Any help is appreciated and would greatly increase my capacity to manage my enterprise

Hi @andres !
I was wondering if you were able to get the append-same-file working.
Thanks!

I’m going to reply to myself here in case anyone needs to address the append file issue.
I found that the responses will append to file if you make the following two updates:

  1. In the script.js file, update the DEFAULT_MODE variable from writeFile to appendFile (line 10 currently)
    DEFAULT_MODE = 'appendFile',

  2. The test script should also be updated to reflect the appendFile mode (currently line 5)
    mode: 'appendFile',

Making these two changes allowed me to use the runner on a collection which has several iterations, all responses were written to the same file.

NOTE: If you implemented “Change the requestName property” to include a date, these two changes will not work. The requestName line should be:
requestName: request.name || request.url,

2 Likes

Hello, @fabianrime6
I also encountered the same problem.
Have you solved it?

Thank you.

@singhsivcan - I tried executing the code as-is and get the following error in the console, please help

Process exited with code 1
C:\Program Files\nodejs\node.exe .\script.js
ResponsesToFile App is listening now! Send them requests my way!
script.js:42
Data is being stored at location: C:\Users\ssenguttuvan\code\ResponseToFile-Postman\Responses
script.js:43
TypeError [ERR_INVALID_ARG_TYPE]: The “data” argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
at Object.writeFile (node:fs:2168:5)
at C:\Users\ssenguttuvan\code\ResponseToFile-Postman\script.js:30:13
at Layer.handle [as handle_request] (C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\express\lib\router\index.js:346:12)
at next (C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\express\lib\router\index.js:280:10)
at urlencodedParser (C:\Users\ssenguttuvan\code\ResponseToFile-Postman\node_modules\body-parser\lib\types\urlencoded.js:100:7)