Trying to merge pdf files in node: PDFUnite + Nodejs - node.js

I have a site on heroku, and am using the HyPDF addon which allows you to manipulate pdf's in a number ways. Specifically, I am trying to merge two pdf's that I have the URL's for.
However, it's not working. I am using request to pull the pdf files and then using request to make the call to PDFUnite. It's a bit of callback hell too, so if you have any suggestions on how to fix that, I would really appreciate it!
Thanks!
request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}, function(err, res, body) {
var pdf1 = body;
request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null}, function(err2, res2, body2){
var pdf2 = body2;
request.post(
'https://www.hypdf.com/htmltopdf',
{
json: {
user: HY_USER,
password: HY_PASS,
file_1: pdf1,
file_2: pdf2
}
},
function (error3, res3, body3) {
if (!error && res2.statusCode == 200) {
console.log('Public URL: ', body3.url);
console.log('Number of pages: ', res3.headers['hypdf-pages']);
}
});
})
});
/****************UDPATED*****************/
I've updated the code, using https://gist.github.com/redfield/6724717 as a guide. The difference is that code sample uses files, whereas I'm using a URL.
I've tried to modify it appropriately, but obviously something is off...If I use body or body.toString('base64') as the pdf1 and pdf2 then I get a 400 status error. Otherwise, if I use the res as the pdf files, then I get a 504 error. I'm not really sure how I should be sending in the files, so it's just guess and check. Appreciate the help!
request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}, function(err, res, body) {
var pdf1 = body;
request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null}, function(err2, res2, body2){
var pdf2 = body2;
var form = new FormData();
form.append('user', HYPDF_USER);
form.append('password', HYPDF_PASSWORD);
form.append('test', 'true');
// form.append('bucket', 'hypdf_test');
form.append('key', 'hypdf_test.pdf');
form.append('public', 'true');
form.append('file1', pdf1);
form.append('file2', pdf2);
form.submit('https://www.hypdf.com/pdfunite', function(err, res) {
console.log('err ', err);
// res – response object (http.IncomingMessage)
console.log(res.statusCode, res.resume());
});
})
});
UPDATE #2
I've updated the code combining my code with #remus' code below. However, I'm still getting an error - "Cannot call method 'hasOwnProperty' of null" on the request.post line. Any thoughts?
request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}, function(err, res, body) {
var pdf1 = body.toString('base64');
request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null}, function(err2, res2, body2){
var pdf2 = body2.toString('base64');
var form = new FormData();
form.append('user', HY_UN);
form.append('password', HY_PASS);
form.append('public', 'true');
form.append('file1', fs.createReadStream(pdf1));
form.append('file2', fs.createReadStream(pdf1));
request.post({
url: 'https://www.hypdf.com/pdfunite',
formData: form
}, function(e, r, body) {
// body should be the binary result of the merged .pdf
console.log('e', e);
console.log('body', body);
});
})
});
UPDATE 3
ERROR STACK for the request.post - I've tried a number of things, can't get rid of it. Any thoughts?
TypeError: Cannot call method 'hasOwnProperty' of null
at appendFormValue (/node_modules/request/request.js:340:17)
at Request.init (/node_modules/request/request.js:354:11)
at new Request (/node_modules/request/request.js:140:8)
at request (/node_modules/request/index.js:55:10)
at Function.post (/node_modules/request/index.js:63:12)
**at Request._callback (/server/api/emailInvoice/emailInvoice.controller.js:34:12)**
at Request.self.callback (/node_modules/request/request.js:198:22)
at Request.emit (events.js:98:17)
at Request.<anonymous> (/node_modules/request/request.js:1073:14)
at Request.emit (events.js:117:20)
at IncomingMessage.<anonymous> (/node_modules/request/request.js:1019:12)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:929:16
at process._tickCallback (node.js:419:13)

First off, the main problem is you're trying to POST binary data (.pdf) as JSON in your last POST request. I imagine that's not going to work - like maybe it expects multipart file data? Second, an easy way to fix up your callback hell is to use node-async:
var request = require('request');
var async = require('async');
async.parallel(function(callback) {
pdf1: function(callback) {
request.get({
url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0'
}, function(err, r, body) {
callback(err, body);
});
},
pdf2: function(callback) {
request.get({
url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557'
}, function(err, r, body) {
callback(err, body);
});
},
function(err, results) {
var formData = {
user: HY_USER,
password: HY_PASS,
file_1: fs.createReadStream(results.pdf1),
file_2: fs.createReadStream(results.pdf2)
}
request.post({
url: 'https://www.hypdf.com/pdfunite',
formData: formData
}, function(e, r, body) {
// body should be the binary result of the merged .pdf
});
});
});
Notice both the fs.createReadStream from the binary data stream, and the proper url using pdfunite.
You might also want to check out the hypdf NPM module - I didn't test it out, but it might make things even easier than manually building up the request.
****UPDATE****
The problem was with using FormData(). For some reason, request.post didn't like it. I was also able to remove the callbacks by injecting the request streams directly into the form. Also note, you have to set encoding to null.
var finalpdf;
var formData = {
'user': HY_USER,
'password': HY_PASSWORD,
'test': 'true',
'public': 'true',
'file1': request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}),
'file2': request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null})
}
request.post({url: 'https://www.hypdf.com/pdfunite', encoding: null, formData: formData}, function(err, res3, body3){
finalPdf = body3.toString('base64');
});

Related

NodeJS - ERR_INVALID_ARG_TYPE Error thrown while issuing a HTTP request to remote host

Recently, I encountered a problem while trying to issue a request using NodeJS and request-promise.
The following code is nested inside a multer call for file uploading (using nested functions / clusters.
const options = {
method: 'POST',
uri: 'URL of your choice',
body: {
//Body of the request
},
// json: true,
headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
},
}
request(options)
.then(function (response) {
console.log('Response: ', response);
})
.catch(function (err) {
console.log('Error: ', err);
});
While using the current request, without the 'json: true' property (commented out), I get the following error:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer. Received type object
at write_ (_http_outgoing.js:603:11)
at ClientRequest.write (_http_outgoing.js:575:10)
at Request.write (PATH/node_modules/request/request.js:1500:27)
at end (PATH/node_modules/request/request.js:549:18)
at Immediate.<anonymous> (PATH/node_modules/request/request.js:578:7)
at runCallback (timers.js:696:18)
at tryOnImmediate (timers.js:667:5)
at processImmediate (timers.js:649:5)
at process.topLevelDomainCallback (domain.js:121:23)
And when I turn the 'json: true' option on, the problem doesn't occur, but the remote API returns an error as it doesn't handle JSON requests/their added curly braces well.
Any ideas about getting over this issue?
Thank you.
Solved it!
As the remote host doesn't handle JSON well, and required "ordinary" POST request to be sent, I looked again inside request-promise's documentation.
By changing body{} to formData{}, and commenting out json: true, the problem was solved.
const options = {
method: 'POST',
uri: 'URL of your choice',
formData: {
//Request's data
},
}
request(options)
.then(function (response) {
console.log('Response: ', response);
})
.catch(function (err) {
console.log('Error: ', err);
});
Try Below -
url = "your url"
const options = {
url: url,
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8'
},
body: {
}
};
request.post(options, function (err, response, body) {
// do something with your data
console.log(response.statusCode);
console.log(body)
});
I faced similar issue, for my case, upload directory was not properly defined, make sure the path to which you want to upload file exists and is clearly defined

calling external rest api from node without encoding querystring params

I am trying to call an external rest API from node server by using request node module.
let request = require('request');
var options = {
method: 'POST',
url: 'https://somerestURI:3000',
qs: { msg: 'some|data|for|other|server' }
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
If I try to run the above code, query string value is being encoded to
some%7cdata%7cfor%7cother%7cserver
as a result I am not receiving correct response.
But if I fire the same request in POSTMAN. I am receiving the expected output(I think postman is not encoding query string).
So what I want is don't encode the query string value.
Any help would be greatly appreciated.
As answered here, you can disable encoding in qsStringifyOptions
var options = {
method: 'POST',
url: 'https://somerestURI:3000',
qs: { msg: 'some|data|for|other|server' },
qsStringifyOptions: {
encoding: false
}
};
You can use node-rest-client package. It allows connecting to any REST API and get results as javascript Object.
var HttpClient = require('node-rest-client').Client;
var httpClient = new HttpClient();
// GET Call
httpClient.get("http://remote.site/rest/xml/method", function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});)
or for POST Call
var args = {
data: { test: "hello" },
headers: { "Content-Type": "application/json" }
};
//POST Call
httpClient.post("http://remote.site/rest/xml/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});

How to properly use putAsync after promisify request module

I searched here and there and ended up with no finding regarding putAsync method of promisified request by bluebird.
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: BU,
workstations: formattedWorkStaions[BU]
})
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
Above is the code snippet that is in my program. And it does not send put request. While using postAsync, if will send post request successfully.
Can anyone help explain why?
I already found the part where is wrong in the putAsync code snippet. I should use json not body as the key for the payload. And the payload does not need to be stringified.
Here is the new code snippet proven to work.
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
json: {
name: BU,
workstations: formattedWorkStaions[BU]
}
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
This is quite tricky and result in my second question.
Why there is such a difference between post and put other than their method type?

Uploading file using POST request in Node.js

I have problem uploading file using POST request in Node.js. I have to use request module to accomplish that (no external npms). Server needs it to be multipart request with the file field containing file's data. What seems to be easy it's pretty hard to do in Node.js without using any external module.
I've tried using this example but without success:
request.post({
uri: url,
method: 'POST',
multipart: [{
body: '<FILE_DATA>'
}]
}, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
Looks like you're already using request module.
in this case all you need to post multipart/form-data is to use its form feature:
var req = request.post(url, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
var form = req.form();
form.append('file', '<FILE_DATA>', {
filename: 'myfile.txt',
contentType: 'text/plain'
});
but if you want to post some existing file from your file system, then you may simply pass it as a readable stream:
form.append('file', fs.createReadStream(filepath));
request will extract all related metadata by itself.
For more information on posting multipart/form-data see node-form-data module, which is internally used by request.
An undocumented feature of the formData field that request implements is the ability to pass options to the form-data module it uses:
request({
url: 'http://example.com',
method: 'POST',
formData: {
'regularField': 'someValue',
'regularFile': someFileStream,
'customBufferFile': {
value: fileBufferData,
options: {
filename: 'myfile.bin'
}
}
}
}, handleResponse);
This is useful if you need to avoid calling requestObj.form() but need to upload a buffer as a file. The form-data module also accepts contentType (the MIME type) and knownLength options.
This change was added in October 2014 (so 2 months after this question was asked), so it should be safe to use now (in 2017+). This equates to version v2.46.0 or above of request.
Leonid Beschastny's answer works but I also had to convert ArrayBuffer to Buffer that is used in the Node's request module. After uploading file to the server I had it in the same format that comes from the HTML5 FileAPI (I'm using Meteor). Full code below - maybe it will be helpful for others.
function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}
var req = request.post(url, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
var form = req.form();
form.append('file', toBuffer(file.data), {
filename: file.name,
contentType: file.type
});
You can also use the "custom options" support from the request library. This format allows you to create a multi-part form upload, but with a combined entry for both the file and extra form information, like filename or content-type. I have found that some libraries expect to receive file uploads using this format, specifically libraries like multer.
This approach is officially documented in the forms section of the request docs - https://github.com/request/request#forms
//toUpload is the name of the input file: <input type="file" name="toUpload">
let fileToUpload = req.file;
let formData = {
toUpload: {
value: fs.createReadStream(path.join(__dirname, '..', '..','upload', fileToUpload.filename)),
options: {
filename: fileToUpload.originalname,
contentType: fileToUpload.mimeType
}
}
};
let options = {
url: url,
method: 'POST',
formData: formData
}
request(options, function (err, resp, body) {
if (err)
cb(err);
if (!err && resp.statusCode == 200) {
cb(null, body);
}
});
I did it like this:
// Open file as a readable stream
const fileStream = fs.createReadStream('./my-file.ext');
const form = new FormData();
// Pass file stream directly to form
form.append('my file', fileStream, 'my-file.ext');
const remoteReq = request({
method: 'POST',
uri: 'http://host.com/api/upload',
headers: {
'Authorization': 'Bearer ' + req.query.token,
'Content-Type': req.headers['content-type'] || 'multipart/form-data;'
}
})
req.pipe(remoteReq);
remoteReq.pipe(res);

nodejs app - download file from sharepoint with NTLM AUTH

I must confess I'm stuck. I need a nodejs app to download a file from a SharePoint library. Simple, huh? No. Not the simple OOTB SharePoint. The only-ssl allowed, with specific mandatory header added and surely only domain-based NTLM authentication method.
I've tried httpntlm (https://www.npmjs.org/package/httpntlm) that seemed to just might work in advance, but no. The SP responses with something went wrong message.
I've tried node-sharepoint, but it doesn't support NTLM yet. The app gets ETIMEDOUT response.
Any ideas, please welcome.
I am able to download the file using httpntlm module.you need to change the few lines of code.Replace the waterfall logic with below code in httpntlm.js.
async.waterfall([
function ($){
var type1msg = ntlm.createType1Message(options);
httpreq.get(options.url, {
headers:{
'Connection' : 'keep-alive',
'Authorization': type1msg
},
agent: keepaliveAgent
}, $);
},
function (res, $){
if(!res.headers['www-authenticate'])
return $(new Error('www-authenticate not found on response of second request'));
var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
var type3msg = ntlm.createType3Message(type2msg, options);
if(method!=='download'){
httpreq[method](options.url, {
headers:{
'Connection' : 'Close',
'Authorization': type3msg
},
allowRedirects: false,
agent: keepaliveAgent
}, $);
}else{
//By Dheeraj for file download
httpreq.download(
url,
{
headers:{
'Connection' : 'Close',
'Authorization': type3msg
},
//allowRedirects: false,
agent: keepaliveAgent
},
__dirname + 'your_filename',
function (err, progress){
if (err) return console.log(err);
console.log(progress);
}, function (err, res){
if (err) return console.log(err);
console.log(res);
});
}
}
], callback);
};
['get', 'put', 'post', 'delete', 'head','download'].forEach(function(method){
exports[method] = exports.method.bind(exports, method);
});
and replace download method of httpreq.js(httpntm_module/node_modules/httpreq_module/httpreq.js) You can find it at Line number 79 approx.
exports.download = function (url,options, downloadlocation, progressCallback, callback) {
//var options = {};
options.url = url;
options.method = 'GET';
options.downloadlocation = downloadlocation;
options.allowRedirects = true;
// if only 3 args are provided, so no progressCallback
if(callback === undefined && progressCallback && typeof(progressCallback)==="function")
callback = progressCallback;
else
options.progressCallback = progressCallback;
doRequest(options, callback);
}
Please let me know if you are still getting issues.

Resources