0

looking for the Suitescript / Javascript experts out there to point me in the right direction here. I am creating a scheduled script that connects to a third party API and the API call requires that a GZIP file is submitted via HTTP.POST using multipart form data. I have tested via CURL so I know that my data is valid. Here is the CURL test:

curl --location --request POST 'https://dev.themarket.co.nz/api/product/sku/sheet/upload' \
--header 'Content-Type: multipart/form-data' \
--header 'Authorization: <tokem goes here>  ' \
--form 'data={"MerchantId": "5233", "Overwrite": 1,"FileFormatCode": "JSON", "SourceChannel": "MERCHANT", "SourceOrg": "Shop Until", "IgnoreExistingImage": 1 }' \
--form 'file=@/Users/tim/inventory.json.gz'

To create something equivalent in Suitescript I am coding the data by hand since Suitescript doesn't have a feature for creating multipart form data. This works well and I can successfully submit the API call to the remote system and the system reads the body data correctly and extracts the parts of the message. BUT the second part of the message is the GZIP file and the contents of the file are corrupt. I can't figure out how to take the file from the filing cabinet in Netsuite and encode the contents in the right way. I think I am missing an encoding step?

Here is my code snippet and below is how the body content looks.

var gz_content = gzippedFile.getContents();
var body = [];   
body.push('--' + boundary);
body.push('Content-Disposition: form-data; name="data"');
body.push('');
body.push(form_data);
body.push('--' + boundary);
body.push('Content-Disposition: form-data; name="file"; filename="inventory.json.gz"');
body.push('Content-Type: application/x-gzip');
body.push('');
body.push(gz_content);     
body.push('--' + boundary + '--');
body.push('');
             
  response = https.post({ 
          url: api_call, 
          headers: headers_themarket,
           body: body.join('\r\n')
  });

And this is what the multipart content looks like:

--70834bf1-1439-4681-b1d4-fb3fa1f9efef

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



{"MerchantId": "5233", "Overwrite": 1,"FileFormatCode": "JSON", "SourceChannel": "MERCHANT", "SourceOrg": "Shop Until", "IgnoreExistingImage": 1 }

--70834bf1-1439-4681-b1d4-fb3fa1f9efef

Content-Disposition: form-data; name="file"; filename="inventory.json.gz"

Content-Type: application/x-gzip



H4sIAAAAAAAC/6tW8swrS80ryS+q9MksLlGyUoiuVgrOLnXOT0kFcpQcXQ0MDIyVdJQcc3LykxNLMvPzgMIWFhY6SgGpRQWpJaWJOUABAx0lH6g0TGdKalpiaU6JUm1sLQCHcFtKZQAAAA==

--70834bf1-1439-4681-b1d4-fb3fa1f9efef--
tgflash
  • 11
  • 2

1 Answers1

0

It looks like you already have the gzipped file. If so, when you use getContents() you are receiving a base64 endcoded value. See In NetSuite with SuiteScript 2.0 unable to send a file with HTTP POST request with a content-type multipart/form-data

you'd need something like:

body.push('Content-Type: application/x-gzip');
body.push('Content-Transfer-Encoding: base64');

It actually looks like you based your code off of that answer. The transfer encoding part was buried in the comments.

bknights
  • 14,408
  • 2
  • 18
  • 31
  • Thank you, that makes a lot of sense. But with that change it still doesn't work. I am creating the GZIP file in the script using N/compress and saving to the file cabinet. If I download the file manually and then upload to the API via CURL this works so I know the file is good. The API accepts the request so I know the multiplart data is well formed. Driving me crazy. – tgflash Oct 28 '20 at 23:47
  • If you use verbose mode with cUrl what is the difference between that and the Netsuite body? – bknights Oct 29 '20 at 17:58