Binary File Support in N/https Module for SuiteScript

Discover how to send binary files using the N/https module in SuiteScript, enhancing your integration capabilities.

·2 min read·11 views·View Oracle Docs

TL;DR Opening

You can now send binary files in SuiteScript using the N/https module. By converting the file contents to a Uint8Array with Uint8Array.fromBase64(), you can include it in the options.body parameter for https.post(options) requests. Each request can handle only one file.

How Does Binary File Support Work?

Starting in NetSuite, the ability to send binary files via the N/https module significantly enhances the usage of web services in SuiteScript. The new approach involves converting file contents into a Uint8Array, which can then be sent as part of an HTTPS POST request. This opens up various possibilities for integrations involving file uploads, such as sending images, documents, or other file types programmatically.

How to Use N/https to Send a Binary File

Here’s a step-by-step guide on how to send a binary file in SuiteScript:

  1. Convert the File: Convert your file data to a Base64 string.
  2. Create a Uint8Array: Utilize Uint8Array.fromBase64() to create a Uint8Array from the Base64 encoded string.
  3. Set Up Options: Prepare the options object which defines the HTTP request settings.
  4. Send the Request: Use https.post(options) to send the request.

Example Code Snippet

Here is a code snippet demonstrating how to send a binary file:

javascript
1"text-purple-400">require(['N/https'], "text-purple-400">function(https) {
2 "text-purple-400">var fileData = '...'; // Base64 encoded data of your file
3 "text-purple-400">var binaryData = Uint8Array.fromBase64(fileData);
4
5 "text-purple-400">var options = {
6 url: 'https://example.com/upload',
7 body: binaryData
8 };
9
10 "text-purple-400">var response = https.post(options);
11 log.debug('Response', response);
12});

Important Considerations

When sending binary data:

  • NetSuite automatically sets the Content-Type to application/octet-stream when using Uint8Array. You may modify this header to suit the file type if necessary.
  • It is not possible to send files with the multipart/form-data content type.
  • Note that the Uint8Array body feature is only supported in SuiteScript 2.1 and later.

Governing and Timeouts

It's crucial to be mindful of the following constraints while using the N/https module:

  • A connection will time out if it takes longer than 5 seconds to establish.
  • If the payload takes over 45 seconds to transmit, it will also time out.
  • This method is not applicable in unauthenticated client-side contexts, limiting its use cases.

Who This Affects

  • Developers: Implementing integrations that require uploading files.
  • Administrators: Managing SuiteScript functionalities and deployment.

Key Takeaways

  • Binary file support in the N/https module enables sending files using Uint8Array in SuiteScript.
  • The https.post(options) method is essential for initiating such requests.
  • Pay attention to timeout settings and the restrictions on Content-Type headers.

Source: This article is based on Oracle's official NetSuite documentation.

Frequently Asked Questions (4)

Is binary file support in the N/https module available in all versions of SuiteScript?
The Uint8Array body feature for binary file support is only available in SuiteScript 2.1 and later.
What file types can be sent using the N/https module's binary file support?
The module can send any type of binary data, but the `Content-Type` is automatically set to `application/octet-stream`. You may need to modify this header to match your specific file type if necessary.
Can I send multiple files in a single HTTPS POST request using the N/https module?
No, each request can handle only one binary file using the options.body parameter in the current implementation.
Do I need to configure additional headers when sending binary data with the N/https module?
NetSuite sets the `Content-Type` to `application/octet-stream` by default for binary data using Uint8Array. However, you may adjust this header to fit the needs of your specific file type.
Source: Binary file support for N/https Module Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in SuiteScript

View all SuiteScript articles →