0

I am trying to post an image form the cabinet to an API

the API is rejected call with Content-Type": "multipart/form-data:

{ httpCode: "405", httpMessage: "Method Not Allowed", message: "PUT, POST require an input body.", errorClass: "Exception" }

the API is rejected call without Content-Type": "multipart/form-data:

{ httpCode: "400", httpMessage: "Bad Request", message: "JSON Input was invalid. Error Message: Syntax error", errorClass: "InvalidArgumentException" }

the current code is:

function ItemImageCreation(){
var itemId = 4;
var payload;

var StringUrl = "https://someURL";
var boundary = '--' + uuidv4();
var files = file.load(1056); // getting the file
var fileContents = files.getContents(); // getting the content
var decodedStr = fromBaseUTF(fileContents); // conversion to Base64
var form_data = "{\"description\": \"Test Image\",\n\"ordering\": 1\n}";

// add the data field
payload = "\n" + boundary + "\n"
  + 'Content-Disposition: form-data; name=\"data\"\n\n'
  + form_data + "\n"
  + boundary + "\n"
  + 'Content-Disposition: form-data; name=\"image\"\n'
  + 'Content-Type: image/jpeg\n\n'
  + decodedStr + "\n"
  + boundary + "--\n\n";
log.debug("payload", payload);
var Header = {"Authorization": "Bearer " + token,
                        "Content-Type": "multipart/form-data; boundary=" + boundary
                        };
try {
  var response = https.post({
    url: StringUrl,
    body: payload,
    headers: Header
  });
  var newSFID = JSON.parse(response.body);
  log.debug("Item Image creation", newSFID);
} catch (e) {
  log.error({ title: 'Failed to submit file', details: (e.message || e.toString()) + (e.getStackTrace ? (' \n \n' + e.getStackTrace().join(' \n')) : '') });
  log.error('ERROR Item Image Creation', JSON.stringify(e));
}

}

using postman, the image is correctly sent: enter image description here enter image description here

I am using a scheduled script, do you see what is wrong or is there a way to know what is send by netsuite?

Komagain
  • 378
  • 1
  • 10

1 Answers1

1

There is an answer here that covers this: In NetSuite with SuiteScript 2.0 unable to send a file with HTTP POST request with a content-type multipart/form-data

what you missing is the Content-Transfer-Encoding header and you should be getting the content as Base64 so you shouldn't need to convert from UTF16 ( I could be wrong on that but I've never needed to)

bknights
  • 14,408
  • 2
  • 18
  • 31
  • thanks for the advise, but as per new tests, I am stucked before the reading of the file, when I add `"Content-Type": "multipart/form-data; boundary=" + boundary`, no body is sent. when I use `"Content-Type": "application/x-www-form-urlencoded"`, the body is sent (but rejected by the server) – Komagain Jan 28 '21 at 21:04
  • 1
    note you have quite a few other issues with your code. Right off the bat I see inconsistent application of line feeds including an initial one on the body etc. The sample has no intial line feeds and use \r\n as the eol marker. The boundary sent in the header should not have the leading '---' characters – bknights Jan 28 '21 at 23:55
  • Thanks @bknights, following our problem with the fact that the bodies are not sent, I have raised a ticket to netsuite, this is the answer: _I have investigated you concern with my senior colleague and we have found out that NetSuite does not support ability to send file using "multipart/form-data" content type to servers accepting the file stream. There has been old cases which had the same issue as this one and the Enhancement has been already crated however, unfortunately, there is no planned date for such a feature to be implemented in near future._ – Komagain Jan 29 '21 at 17:01
  • thanks to your code, the data is sent, the format is fine, thank you. I am still encountering an issue, I don't know if it related to the data or a specificity of the API target: `Invalid image type text/plain; charset=us-ascii for facebook.png"`, when I compare what is sent from netsuite to what is sent form postman, I don't see any difference (I am using pipedream to check the form data) – Komagain Jan 30 '21 at 20:28