2017-10-26 1 views
0

ich nicht in der Lage bin zu senden meine „multipart/form-data‘ Datei auf diese API zu senden. Wenn ich POSTMAN verwenden .? es funktioniert, aber mit der https post-Methode scheint es, dass NetSuite nicht die „form-data“ Content-Type erkennt jemand weiß, wie man ein Formular-Daten mit SuiteScript 2 ist ein Teil meines Codes Hier senden:In NetSuite mit SuiteScript 2.0 nicht in der Lage, eine Datei mit HTTP-POST-Anforderung mit einer Content-Typ multipart/form-data

var fileObj = file.create({ 
       name: invoiceNumber + '_ubl.xml', 
       fileType: file.Type.XMLDOC, 
       contents: einvoicecontentwithpdf, 
       folder : 120, 
       isOnline : false 
      }); 

var headers = { 
        'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxx', 
        'Content-Type': 'multipart/form-data' 
      }; 

      var response = https.post({ 
       url: 'https://community-api-uat-1-2.nxt.uat.unifiedpost.com/api/universal_connector/v1/uc/ar', 
       body: { 
        'file': fileObj      
       }, 
       headers: headers 
      }); 

Antwort

3

Sie haben sich den ganzen Körper zu bauen.

Dieses so viel arbeitet wie ich bisher getestet habe. Code ein wenig nicht-idomatic aussehen kann, weil sie von Typoskript kompiliert.

Beachten Sie, dass diese Nicht-Textdateien enthalten müssten angepasst werden, um base64 die Dateiinhalte kodieren.

/** 
* multiPartUpload.js 
* @NApiVersion 2.x 
*/ 
define(["require", "exports", "N/https", "N/file", "N/log"], function (require, exports, http, file, log) { 
    Object.defineProperty(exports, "__esModule", { value: true }); 
    var types = {}; 
    types[file.Type.AUTOCAD] = 'application/x-autocad'; 
    types[file.Type.BMPIMAGE] = 'image/x-xbitmap'; 
    types[file.Type.CSV] = 'text/csv'; 
    types[file.Type.EXCEL] = 'application/vnd.ms-excel'; 
    types[file.Type.FLASH] = 'application/x-shockwave-flash'; 
    types[file.Type.GIFIMAGE] = 'image/gif'; 
    types[file.Type.GZIP] = 'application/?x-?gzip-?compressed'; 
    types[file.Type.HTMLDOC] = 'text/html'; 
    types[file.Type.ICON] = 'image/ico'; 
    types[file.Type.JAVASCRIPT] = 'text/javascript'; 
    types[file.Type.JPGIMAGE] = 'image/jpeg'; 
    types[file.Type.JSON] = 'application/json'; 
    types[file.Type.MESSAGERFC] = 'message/rfc822'; 
    types[file.Type.MP3] = 'audio/mpeg'; 
    types[file.Type.MPEGMOVIE] = 'video/mpeg'; 
    types[file.Type.MSPROJECT] = 'application/vnd.ms-project'; 
    types[file.Type.PDF] = 'application/pdf'; 
    types[file.Type.PJPGIMAGE] = 'image/pjpeg'; 
    types[file.Type.PLAINTEXT] = 'text/plain'; 
    types[file.Type.PNGIMAGE] = 'image/x-png'; 
    types[file.Type.POSTSCRIPT] = 'application/postscript'; 
    types[file.Type.POWERPOINT] = 'application/?vnd.?ms-?powerpoint'; 
    types[file.Type.QUICKTIME] = 'video/quicktime'; 
    types[file.Type.RTF] = 'application/rtf'; 
    types[file.Type.SMS] = 'application/sms'; 
    types[file.Type.STYLESHEET] = 'text/css'; 
    types[file.Type.TIFFIMAGE] = 'image/tiff'; 
    types[file.Type.VISIO] = 'application/vnd.visio'; 
    types[file.Type.WORD] = 'application/msword'; 
    types[file.Type.XMLDOC] = 'text/xml'; 
    types[file.Type.ZIP] = 'application/zip'; 
    function getContentType(f) { 
     var mime = types[f.fileType]; 
     var charset = f.encoding; 
     var ct = 'Content-Type: ' + mime + (charset ? ';charset=' + charset : ''); 
     log.debug({ title: 'content for ' + f.name, details: ct }); 
     return ct; 
    } 
    function isFile(o) { 
     return (typeof o == 'object' && typeof o.fileType != 'undefined'); 
    } 
    /** 
    * Creates a multipart upload 
    * @param {string} url  to post to 
    * @param {object} headers key/value of headers; include Auth headers if needed 
    * @param {array} parts array of {name:string, value:file|string} 
    */ 
    function uploadParts(url, headers, parts) { 
     var boundary = 'someuniqueboundaryasciistring'; 
     headers['content-type'] = 'multipart/form-data; boundary=' + boundary; 
     // Body 
     var body = []; 
     parts.forEach(function (p, idx) { 
      var partIsFile = isFile(p.value); 
      body.push('--' + boundary); 
      body.push('Content-Disposition: form-data; name="' + p.name + '"' + (partIsFile ? ('; filename="' + p.value.name + '"') : '')); 
      if (partIsFile) { 
       body.push(getContentType(p.value)); 
      } 
      body.push(''); 
      body.push(partIsFile ? p.value.getContents() : p.value); 
      if (idx == parts.length - 1) { 
       body.push('--' + boundary + '--'); 
       body.push(''); 
      } 
     }); 
     // Submit Request 
     try { 
      var response = http.post({ 
       url: url, 
       headers: headers, 
       body: body.join('\r\n') 
      }); 
      return response; 
     } 
     catch (e) { 
      log.error({ title: 'Failed to submit file', details: (e.message || e.toString()) + (e.getStackTrace ? (' \n \n' + e.getStackTrace().join(' \n')) : '') }); 
     } 
    } 
    exports.uploadParts = uploadParts; 
    ; 
}); 

ein einfacher Test:

/** 
*@NApiVersion 2.x 
*@NScriptType Suitelet 
*/ 
define(["require", "exports", "N/file", "./multiPartUpload"], function (require, exports, file, multiPartUpload_1) { 
    Object.defineProperty(exports, "__esModule", { value: true }); 
    function onRequest(context) { 
     var files = [ 
      { name: 'f1', value: file.load({ id: 1056 }) }, // file cabinet ids; you can use dynamic files 
      { name:'t1', value:'test'}, 
      { name: 'f2', value: file.load({ id: 7479 }) } 
     ]; 
     var resp = multiPartUpload_1.uploadParts('https://your-test-server/', {}, files); 
     context.response.write({ output: resp.body }); 
    } 
    exports.onRequest = onRequest; 
}); 
+0

Perfect. Es funktioniert, sind Sie erstaunlich :) Aber es ist verrückt, dass NetSuite erlaubt es uns nicht, dass die nativ – hesiode

+0

gut zu tun, die native ist :-) Aber ja diese Outbound http Einschränkungen seit Jahren aufreizend. – bknights

+0

Hallo @bknights, warum haben Sie verwenden: Object.defineProperty (Exporte, "__esModule", {Wert: true}); – user4076248

Verwandte Themen