cja
cja

Reputation: 10016

How to get a NetSuite file as a byte array in a SuiteScript 2 Suitelet?

I am sending data from a NetSuite Suitelet to the Square Connect API. I am using SuiteScript 2.

I need to send an image file from the NetSuite file cabinet to Square via an HTTP POST. The data needs to be sent as binary data in a multipart form, according to the Square documentation at https://docs.connect.squareup.com/api/connect/v2#endpoint-v1items-uploaditemimage.

I think I've got the multipart form working, thanks to https://stackoverflow.com/a/46964827/127434.

However, I haven't worked out how to get the file data into a binary format that will be accepted by Square. Square complains: "Invalid multipart form data".

I will be happy to receive suggestions.

Upvotes: 1

Views: 1875

Answers (1)

bknights
bknights

Reputation: 15402

I believe file.getContents() returns the base64 encoding for binary files.

So in your mulitpart setup try setting the Content-Transfer-Encoding before the file content data.

see https://www.drupal.org/project/smtp/issues/2909678

so in my answer you referenced above you'd try:

if (partIsFile) {
    body.push(getContentType(p.value));
    if(partIsBinary(p.value)) body.push('Content-Transfer-Encoding:base64');
}

Upvotes: 2

Related Questions