Search code examples
node.jsrequestibm-cloudobject-storage

Auto append unwanted character in nodejs put request


I have an api to file upload on Bluemix Object Storage by using request module. All are good but there is some unwanted character which append automatically.

example:  
--38oi85df-b5d1-4d42-81ce-c547c860b512 //this is unwanted character
  Email 
 [email protected]
 [email protected] 
 [email protected]
--38oi85df-b5d1-4d42-81ce-c547c860b512-- // this is unwanted character

Here is my code:-

import request from 'request';

exports.putObjectStorageFile = function(authToken, file, csv, cb) {  
var s = new stream.Readable();   
s._read = function noop() {}; 
s.push(csv); //csv is string   
s.push(null);   
var options = {
    url: 'https://xxxx.objectstorage.open.xxxx.com/v1/AUTH_' + config.objectStorage.projectId + '/xxxx/' + file,
    method: 'PUT',
    preambleCRLF: true,
    postambleCRLF: true,
    encoding: 'utf-8',
    headers: {
      'Content-Type': 'text/html; charset=UTF-8',
      'Content-Length': 1,
      'X-Auth-Token': authToken
    },
    multipart: {
      chunked: false,
      data: [
        { body: s }
      ]
    }   };

  function callback(error, response) {
    if (error) cb(error);
    if (!error && response.statusCode == 201) {
      cb(null);
    } 
  }   
request(options, callback); 

Solution

  • I got that lines due to sending data with multipart. I found a solution by adding just content-type,content-lenght and send data in body i.e,

    var options = {
        url: 'https://dal.objectstorage.open.softlayer.com/v1/AUTH_' + config.objectStorage.projectId + '/nrich-storage/' + file,
        method: 'PUT',
        headers: {
          'Content-Type': 'text/csv',
          'Content-Length': csv.length,
          'X-Auth-Token': authToken
        },
        body:csv
      };
    
      function callback(error, response) {
        if (error) cb(error);
        if (!error && response.statusCode == 201) {
          cb(null);
        }
      }
    
    request(options, callback);