Getting Error when create package
Wednesday, May 12, 2021 at 07:14amGetting error when create package when read file from server.
Working Code: When read file from local machine
var options = {
'method': 'POST',
'url': 'https://sandbox.esignlive.com/api/packages',
'headers': {
'Authorization': 'Basic XXXXXXxxxxx',
'Accept': 'application/json'
},
formData: {
'file': {
'value': fs.createReadStream('./documents/pdf/TestFile.pdf'),
'options': {
'filename': './documents/pdf/TestFile.pdf',
'contentType': null
}
},
'payload': JSON.stringify(jsonConvert.serialize(_objClassobject))
}
};
Error in code when read file from server :
var options = {
'method': 'POST',
'url': 'https://sandbox.esignlive.com/api/packages',
'headers': {
'Authorization': 'Basic XXXXXXxxxxx',
'Accept': 'application/json'
},
formData: {
'file': {
'value': fs.createReadStream('https://xxxx/testFile.pdf'),
'options': {
'filename': 'https://xxxx/testFile.pdf',
'contentType': null
},
'payload': JSON.stringify(jsonConvert.serialize(_objClassobject))
}
};
Reply to: Getting Error when create package
Wednesday, May 12, 2021 at 09:04amHi Manish,
As per this StackOverflow post:
https://stackoverflow.com/questions/65976322/how-to-create-a-readable-stream-from-a-remote-url-in-nodejs
Seems the fs.createReadStream() function does not work with http URLs, only file:// URLs or filename paths.
Hence, you'd use the similar trick we've discussed last time, downloading the http resource first as a Buffer and send to the Post request, similar to below:
var bufs = [];
const writableStream = Stream.Writable()
writableStream._write = (chunk, encoding, next) => {
bufs.push(Buffer.from(chunk))
next()
}
var stream = request('https://xxxx/test.pdf').pipe(writableStream)
stream.on('finish', function(){
var b = Buffer.concat(bufs); //b with type of Buffer
var options =
{
method: 'POST',
url: 'https://sandbox.esignlive.com/api/packages',
headers:
{
accept: 'application/json',
authorization: 'Basic ' + api_key,
'content-type': 'multipart/form-data; boundary=---011000010111000001101001' },
formData:
{
file:
{
value: b,
options: { filename: 'sample_contract.pdf', contentType: null }
},
"payload": jsonPayload }
};
request(options, function (error, response, body)
{
if (error) throw new Error(error);
var tmp = JSON.parse(body);
packageid = tmp['id'];
console.log("The package id is: " + packageid);
});
});
Duo
Reply to: Hi Manish, As per this…
Wednesday, May 12, 2021 at 10:48amThank you!!