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.
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
POST http://localhost:3000/write50031 ms
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
Request Body
The console only shows bodies smaller than 10 KB inline. To view the complete body, inspect it by clicking Open.
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
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
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âŠ}
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!
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?