ManishRautela

Getting Error when create package

0 votes

Getting 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

0 votes

Hi 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

Duo Liang OneSpan Evangelism and Partner Integrations Developer


Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off