How to download file while running collection runner?

I have a get request which shows the response in octet stream. When i run send and download for a particular request it works fine but I want to run that in collection runner.

So please help me with that.

The solution provided here should work Download file when send request

If it doesn’t please provide more information as to what doesn’t work as expected, if you’re getting any error message etc.

It is not printing my data in docx files and there is no files in the response.

It is showing code 500 internal server error

:arrow_forward:POST http://localhost:3000/write50031 ms

  • :arrow_forward:Network

:arrow_forward:Request Headers

Content-Type: application/octet-stream

User-Agent: PostmanRuntime/7.25.0

Accept: /

Cache-Control: no-cache

Postman-Token: 10dd3644-4d1c-4008-8227-2b47b0320d56

Host: localhost:3000

Accept-Encoding: gzip, deflate, br

Connection: keep-alive

Content-Length: 245934

:arrow_forward:Request Body

The console only shows bodies smaller than 10 KB inline. To view the complete body, inspect it by clicking Open.

:arrow_forward:Response Headers

X-Powered-By: Express

Content-Security-Policy: default-src ‘self’

X-Content-Type-Options: nosniff

Content-Type: text/html; charset=utf-8

Content-Length: 1270

Date: Wed, 03 Jun 2020 11:23:09 GMT

Connection: keep-alive

:arrow_forward:Response Body

hey can you help me real fast, I am in dire need.

A 500 internal server error` means there is an issue with the nodejs application server that is running. Could you share the logs from the server? That would show what went wrong.

These are the logs on postman and these are same as on cmd.

POST /write HTTP/1.1
Content-Type: application/octet-stream
User-Agent: PostmanRuntime/7.25.0
Accept: /
Cache-Control: no-cache
Postman-Token: 435fd453-7760-41c5-b0c7-ccf8ba27f93a
Host: localhost:3000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 245934
The console only shows request bodies smaller than 10 KB inline. To view the complete body, inspect it by clicking Open.
HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Content-Security-Policy: default-src ‘self’
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 1355
Date: Thu, 04 Jun 2020 13:23:36 GMT
Connection: keep-alive

Error
ReferenceError: filePath is not defined
   at C:\Users\Bharat\Desktop\Aelum files\script.js:33:14
   at Layer.handle [as handle_request] (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\layer.js:95:5)
   at next (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\route.js:137:13)
   at Route.dispatch (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\route.js:112:3)
   at Layer.handle [as handle_request] (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\layer.js:95:5)
   at C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\index.js:281:22
   at Function.process_params (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\index.js:335:12)
   at next (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\index.js:275:10)
   at urlencodedParser (C:\Users\Bharat\Desktop\Aelum files\node_modules\body-parser\lib\types\urlencoded.js:100:7)
   at Layer.handle [as handle_request] (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\layer.js:95:5)

{id: “84c7929a-31ac-478e-81ac-0d1d08c8267b”, status: “Internal Server Error”, code: 500…}

  1. id: “84c7929a-31ac-478e-81ac-0d1d08c8267b”
  2. status: “Internal Server Error”
  3. code: 500
  4. :arrow_forward:header: [7]
  5. :arrow_forward:0: {…}
    1. key: “X-Powered-By”
    2. value: “Express”
  6. :arrow_forward:1: {…}
    1. key: “Content-Security-Policy”
    2. value: “default-src ‘self’”
  7. :arrow_forward:2: {…}
    1. key: “X-Content-Type-Options”
    2. value: “nosniff”
  8. :arrow_forward:3: {…}
    1. key: “Content-Type”
    2. value: “text/html; charset=utf-8”
  9. :arrow_forward:4: {…}
    1. key: “Content-Length”
    2. value: “1355”
  10. :arrow_forward:5: {…}
    1. key: “Date”
    2. value: “Thu, 04 Jun 2020 13:23:36 GMT”
  11. :arrow_forward:6: {…}
    1. key: “Connection”
    2. value: “keep-alive”
  12. :arrow_forward:stream: {…}
  13. type: “Buffer”
  14. :arrow_forward:data: [1355]
  15. cookie: [0]
  16. responseTime: 11
  17. responseSize: 1355

Data is being stored at location: C:\Users\Bharat\Desktop\Aelum files\Responses
ReferenceError: filePath is not defined
at C:\Users\Bharat\Desktop\Aelum files\script.js:33:14
at Layer.handle [as handle_request] (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (C:\Users\Bharat\Desktop\Aelum files\node_modules\body-parser\lib\types\urlencoded.js:100:7)
at Layer.handle [as handle_request] (C:\Users\Bharat\Desktop\Aelum files\node_modules\express\lib\router\layer.js:95:5)

This is the script file i am using

const express = require(‘express’),
app = express(),
fs = require(‘fs’),
shell = require(‘shelljs’),
// Modify the folder path in which responses need to be stored
folderPath = ‘./Responses/’,
defaultFileExtension = ‘docx’, // Change the default file extension
bodyParser = require(‘body-parser’),
DEFAULT_MODE = ‘writeFile’,
path = require(‘path’);
// Create the folder path in case it doesn’t exist
shell.mkdir(’-p’, folderPath);
// Change the limits according to your response size
app.use(bodyParser.json({limit: ‘150mb’, extended: true}));
app.use(bodyParser.urlencoded({ limit: ‘150mb’, extended: true }));
app.get(’/’, (req, res) => res.send(‘Hello, I write data to file. Send them requests!’));
app.post(’/write’, (req, res) => {
let extension = req.body.fileExtension || defaultFileExtension,
fsMode = req.body.mode || DEFAULT_MODE;
uniqueIdentifier = req.body.uniqueIdentifier ? typeof req.body.uniqueIdentifier === ‘boolean’ ? Date.now() : req.body.uniqueIdentifier : false;
filePath = ${path.join(folderPath, req.body.requestName)}.${extension};
// Use a unique identifier if it exists
if (uniqueIdentifier) {
filePath = ${filePath}${uniqueIdentifier};
}
fs[fsMode](filePath, req.body.responseData, (err) => {
if (err) {
console.log(err);
res.send(‘Error’);
}
else {
res.send(‘Success’);
}
});
});
app.listen(3000, () => {
console.log(‘ResponsesToFile App is listening now! Send them requests my way!’);
console.log(Data is being stored at location: ${path.join(process.cwd(), folderPath)});
});

This is the test case in postman

// The opts for the server, also includes the data to be written to file
let opts = {
requestName: request.name || request.url,
fileExtension: ‘docx’,
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/octet-stream’,
body: {
mode: ‘raw’,
raw: JSON.stringify(opts)
}
}, function (err, res) {
console.log(res);
});

If i use application\octet-stream it is not working but when i convert it to application\json then it prints the file but not in readable mode. the docx file is not opening it is saying error opening contents of file.

How can i solve that?

@bharat.r That collection doesn’t seem to work with binary data. I’d recommend using Newman instead, @vdespa has written an article that explains how to achieve this:

Hope that helps!

1 Like

Sir ,
this is showing to write all the files but i just want to write a file for particular request.

It will only write one file if you run it on a collection with a single request.

what to do if i have multiple requests in a collection?