0

I am trying to download a file from jira server and storing in my file but i am not able to store it in my file saying below error:

(data.body).pipe(fs.WriteStream('file.xlsx'));

          ^

TypeError: undefined is not a function

my nodejs code:

var express = require('express');
var app = express();
var request = require('request');
var bodyParser     =        require("body-parser");
var fs = require('fs');
var https = require('https');

app.use(bodyParser.urlencoded({ extended: false }));
require('ssl-root-cas/latest').inject();

var credentials = 'user:pass';
var encodedCredentials = new Buffer(credentials).toString('base64')

var url = ' https://gec-jira01.example.com/secure/attachment/IWREQ-373_update.xlsx';    

request({
    "method": "GET", 
    "rejectUnauthorized": false, 
    "url": url, 
    "headers" : {"Content-Type": "application/json",
    "Authorization": "Basic"+' '+encodedCredentials}
}, function(err, data, body) {
       //console.log(data);
       (data.body).pipe(fs.WriteStream('file.xlsx'));
       console.log(data.body);
)};
2
  • 1
    you should pipe the request, not the body. As: request({ method: "GET", "rejectUnauthorized": false, "url": url, "headers" : {"Content-Type": "application/json", "Authorization": "Basic"+' '+encodedCredentials} }).pipe(fs.WriteStream('file.xlsx')); Commented Jul 30, 2015 at 12:05
  • but data is in body how to make it trough request Commented Jul 30, 2015 at 12:10

1 Answer 1

2

Try this:

request({
    method: "GET", 
    "rejectUnauthorized": false, 
    "url": url, 
    "headers" : {"Content-Type": "application/json",
    "Authorization": "Basic"+' '+encodedCredentials}
}).pipe(
   fs.createWriteStream('file.xlsx')
);

or this:

request({
    method: "GET", 
    "rejectUnauthorized": false, 
    "url": url, 
    "headers" : {"Content-Type": "application/json",
    "Authorization": "Basic"+' '+encodedCredentials}
},function(err,data,body) {
   fs.WriteStream('file.xlsx').write(body);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.