Monday, October 10, 2016

Moved from Restler to Needle

I had two web services  one to download file and another one to upload files.
I started coding for uploading a file using restler module. which worked absolutely fine. then when I started coding for downloading file it was corrupting the binary stream file.

I started looking for different options, I had  two options 'request' and 'needle'.

I worked with 'request v2.73.0' module for 2 months. but after updating to v2.75.0. It just stopped working I was not even able to make regular POST request. 

After that, I had to look for another option than I started using Needle module.
which solved my both purpose of uploading and downloading files.  


File Download using Needle
var needle = require('needle'); var dataToSend = { data1: "data1", data2: "data2"}; needle .post('webServiceURL', dataToSend) .on('end', function (response) { console.log('success log'); res.download('public/uploads/report.pdf'); }).on('error', function (response) {     console.log('Error downloading report file'); }).pipe(fs.createWriteStream('public/uploads/report.pdf'));



File Upload using Needle

var needle = require('needle'); var dataToSend = { data1: "data1", data2: "data2", file2: {file: FILE_PATH,content_type : FILE_TYPE} }; needle .post('webServiceCall', dataToSend, { multipart: true }, function(err, resp, body){ console.log('error: '+err); console.log('response: '+resp); console.log('body: '+body); res.redirect('/fileUploadPage'); });


// web service response in Java should be set in following manner

ResponseBuilder response = Response.ok((Object) fileObject);
response.header("Content-Type", MediaType.APPLICATION_OCTET_STREAM);
response.header("Content-Disposition", "attachment;  filename=\"file_name.pdf\"");
return response.build();




No comments:

Post a Comment