I'm trying to create a Gmail add-on using Apps Script. This calls a third-party web API created in C# to post the email content to another system.
The API method uses POST, and accepts JSON formatted data in the body as parameter.
I can now post messages without attachments.
I there is a parameter for attachments in the method, defined as: public byte[] paramName {get; set;}.
I thought that I can use the getBytes() method, and send its return as parameter to the method.
Code snippet:
var attachments = message.getAttachments();
var lists = [];
for(var i = 0; i < attachments.length; i ){
var attachment = {
"Id": id,
"FileByte": attachments[i].getBytes(),
"FileName": attachments[i].getName()
}
lists.push(attachment);
}
var data = {
"Email": email,
"Comment" : byteMsg,
"Id": id,
"List": lists, // attachments here
"Type": type
}
var url = urlAPI
var options = {
method: 'post',
contentType: 'application/json',
muteHttpExceptions: true,
headers: header,
payload: JSON.stringify(data)
};
Inspecting the logs shows the data (truncated due to length): Logging output too large. Truncating output. [-119.0, 80.0, 78.0, 71.0, 13.0, 10.0, 26.0, 10.0, 0.0, 0.0, 0.0, 13.0, 73.0, 72.0, 68.0, 82.0, 0.0, 0.0, 0.0, -56.0, 0.0, 0.0, 0.0, -6.0, 8.0, 6.0, 0.0, 0.0, 0.0, -28.0, -22.0, 8.0, 56.0, 0.0, 0.0, 0.0, 9.0, 112.0, 72.0, 89.0, 115.0, 0.0, 0.0, 18.0, 116.0,...
There are negative values which I'm not sure if they are valid.
I get an 'Object reference not set to an instance of an object' error. Which was similar to the error I was getting with Comment parameter above (also a byte[]) when I was figuring out how it works.
Has anyone made something or exactly like this? Any help would be appreciated.
Thanks in advance!
CodePudding user response:
From your following situation in your question,
Inspecting the logs shows the data (truncated due to length): Logging output too large. Truncating output. [-119.0, 80.0, 78.0, 71.0, 13.0, 10.0, 26.0, 10.0, 0.0, 0.0, 0.0, 13.0, 73.0, 72.0, 68.0, 82.0, 0.0, 0.0, 0.0, -56.0, 0.0, 0.0, 0.0, -6.0, 8.0, 6.0, 0.0, 0.0, 0.0, -28.0, -22.0, 8.0, 56.0, 0.0, 0.0, 0.0, 9.0, 112.0, 72.0, 89.0, 115.0, 0.0, 0.0, 18.0, 116.0,...
There are negative values which I'm not sure if they are valid.
I thought that in your situation, you might be required to use the unit8array. When the byte array is retrieved from the blob using Google Apps Script, it's the int8array. In order to convert from the int8array to the unit8array, how about the following modification?
From:
"FileByte": attachments[i].getBytes(),
To:
"FileByte": Uint8Array.from(attachments[i].getBytes()),