Mastering Multipart Post Requests with Files in NetSuite Integrations

March 28, 2024 | 4 minute read
Daniel Goldenberg
Senior SDN Software Engineer
Text Size 100%:

Introduction: Bridging NetSuite with External Systems Through File Transfers

In the vast landscape of enterprise operations, the journey of data and documents often extends beyond the confines of NetSuite, reaching into the realms of third-party platforms. Whether it's dispatching purchase invoices to clients or forwarding payment directives to financial institutions, these transactions sometimes necessitate the transmission of a file accompanied by structured data. A prime scenario involves E-invoicing portals that require both the invoice document and its associated data. The technical solution? Multipart/form-data post requests.

This post delves into the multipart/form-data request paradigm, particularly when it involves sending files and text fields in unison.

Multipart/form-data Request Example

Consider the following simplified representation of what a multipart/form-data request could look like when sending both a file and a text field:
plaintextCopy code

POST /submit-form HTTP/1.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="text_field"

Content-Disposition: form-data; name="file_field"; filename="invoice.pdf"
Content-Type: text/plain

(contents of invoice.pdf)

In this exploration, we aim to shed light on essential considerations surrounding multipart/form-data post requests, drawing from practical examples and NetSuite's capabilities.

Documentation and Technical Support Insights

Diving into NetSuite's documentation reveals a crucial note on the limitations of the method:

Note: "Sending a file using multipart/form-data content type in the options.body parameter is not supported." NetSuite Help Center Documentation

However, this doesn't spell the end of the road for multipart/form-data requests within NetSuite. Let's unpack the strategies to navigate this constraint.

Manual Assembly of Multipart Requests

It is feasible to dispatch multipart/form-data requests by "manually" assembling the body and headers to craft a correctly formatted request. Here's a demonstration of how to read a file from the file cabinet and send it via using multipart:
plaintextCopy code

require(['N/https', 'N/file', 'N/encode'], function (https, file, encode){

        let structured_content = '...some info...'
        let headers = {}

        headers = {
            "Authorization":"Bearer some_token",
            "Content-Type":"multipart/form-data; boundary=WebKitFormBoundary7MA4YWxkTrZu0gW"
        let endPoint = ''
        let fileToSend = file.load( { id: 1234 } ) 

        let myFileContent = fileToSend.getContents()

        let body = []
        let boundary = 'WebKitFormBoundary7MA4YWxkTrZu0gW'
        body.push('--' + boundary);
        body.push('Content-Disposition: form-data; name="text_field"');
        body.push('--' + boundary);
        body.push('Content-Disposition: form-data; name="Archivo"; filename="invoice.txt"');
        body.push('Content-Type: text/plain');
        body.push('--' + boundary + '--');

        var response = {};
        var requestResponse = {
            url: endPoint,
            body: body.join( '\r\n' ),
            headers: headers
        } );
        if ( requestResponse.code ) {
            response = requestResponse.body;

File Extension Considerations

The nature of the file (e.g., .pdf vs. .txt) significantly impacts the handling process:
•    PDF Files: Attempting to send a .pdf file results in receiving a base64 encoded version rather than the file itself.
•    Text Files: Conversely, .txt files, being ASCII-based, are retrieved and transmitted in their raw form.

This distinction underscores the need for base64 encryption of .txt files prior to transmission, should the use case require it.

Transmitting Byte-Based Files

It's pertinent to note that NetSuite's method does not support the transmission of byte-based files in multipart/form-data format. This limitation necessitates external solutions for decoding base64 files back into their byte equivalence.

Fortunately, NetSuite boasts a plethora of partners capable of receiving a multipart/form-data request containing a base64-encoded file, decoding it, and then reassembling the request with the file in byte format for transmission to the intended third-party system.

Summary: Navigating File Transmissions with NetSuite

This post has traversed the landscape of sending files using multipart/form-data requests within NetSuite's ecosystem. We've illuminated the path for handling ASCII and base64 encoded files, highlighting the critical nuances associated with different file types. Remember, while the process may require some manual effort and external partnerships, integrating NetSuite with third-party systems through file transfers remains a viable and powerful capability.
Embrace the journey of mastering multipart post requests, and unlock new potentials in your NetSuite integrations.

Daniel Goldenberg

Senior SDN Software Engineer

Daniel Goldenberg has over 15 years of experience in consultancy, product management, and engineering for technology and cloud companies of all sizes.Daniel holds an M.B.A. from the University of Barcelona and a B.S. in Electronic Engineering and Software Development from Universidad ORT Uruguay.

Previous Post

New Developer Updates to Build Intelligent Applications

Maywun Wong | 5 min read