Reputation: 388
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:
I am using a scheduled script, do you see what is wrong or is there a way to know what is send by netsuite?
Upvotes: 0
Views: 1634
Reputation: 15437
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)
Upvotes: 1