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--