在带有 SuiteScript 2.0 的 NetSuite 中,无法使用内容类型为 multipart/form-data 的 HTTP POST 请求发送文件 [英] In NetSuite with SuiteScript 2.0 unable to send a file with HTTP POST request with a content-type multipart/form-data

查看:22
本文介绍了在带有 SuiteScript 2.0 的 NetSuite 中,无法使用内容类型为 multipart/form-data 的 HTTP POST 请求发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将我的multipart/form-data"文件发送到这个 API.如果我使用 POSTMAN 它可以工作,但是使用 https post 方法似乎 netsuite 无法识别form-data"内容-类型.有人知道如何使用 SuiteScript 2 发送表单数据吗?这是我的代码的一部分:

I am not able to send my "multipart/form-data' file to this API. If I use POSTMAN it's working but with the https post method it seems that netsuite doesn't recognize the "form-data" content-type. Somebody knows how to send a form-data with SuiteScript 2 ? Here is a part of my code:

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 
            }); 

推荐答案

你必须自己构建整个身体.

You have to build the whole body yourself.

这与我迄今为止测试过的一样有效.代码可能看起来有点不习惯,因为它是从 Typescript 编译而来的.

This works as much as I've tested so far. Code may look a little non-idomatic because it's compiled from Typescript.

请注意,要包含非文本文件,这必须适应 base64 编码文件正文.

Note that to include non-text files this would have to be adapted to base64 encode the file bodies.

/**
 * 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) { 
                var ct = getContentType(p.value);
                body.push(getContentType(p.value));
                if(partIsBinary(ct)){
                    body.push('Content-Transfer-Encoding: base64');
                }
            }
            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('
')
            });
            return response;
        }
        catch (e) {
            log.error({ title: 'Failed to submit file', details: (e.message || e.toString()) + (e.getStackTrace ? (' 
 
' + e.getStackTrace().join(' 
')) : '') });
        }
    }
    exports.uploadParts = uploadParts;
    ;
});

一个简单的测试:

/**
 *@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;
});

这篇关于在带有 SuiteScript 2.0 的 NetSuite 中,无法使用内容类型为 multipart/form-data 的 HTTP POST 请求发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆