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",
"ordering": 1
}";
// add the data field
payload = "
" + boundary + "
"
+ 'Content-Disposition: form-data; name="data"
'
+ form_data + "
"
+ boundary + "
"
+ 'Content-Disposition: form-data; name="image"
'
+ 'Content-Type: image/jpeg
'
+ decodedStr + "
"
+ boundary + "--
";
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 ? ('
' + e.getStackTrace().join('
')) : '') });
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?
question from:
https://stackoverflow.com/questions/65921100/suitescript-post-an-image-form-the-cabinet-multipart-form-data 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…