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

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,

1 Like

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)

@orbital-module-astro

The error is telling you that the “data” variable is undefined. (Has no data).

The data variable is in the node.js application but I suspect is based on the data being sent by the sendRequest in the collection tests tab.

This is the code for the sendRequest, so you can see how the request is built up and what its sending.

// Please read the documentation on the right side (collection level documentation) to learn about how to use this collection -> 

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

Therefore is your request working ok, and returning data?

When this fires, you should see the sendRequest in your console log, so have a look there.
Does the request look ok? Does the body have the request info in it?