I am trying to create a UrlFetchApp() payload from multiple documents for a third-party service. If I follow their example for multiple inline documents, the UrlFetchApp() payload should contain both binary (for each file) and text (to separate the two pieces of content). How to I supply this mixed content to the payload advanced argument?
I have successfully done binary only using .getBytes() on a blob as follows:
function testfax() {
var faxnum = '12125551234';
var url = 'https://rest.interfax.net/outbound/faxes?faxNumber=+'+faxnum;
Logger.log('url='+url)
Logger.log(" ")
var varAuthString = AuthString(); //user function to create HTTP simple authorization header
//Logger.log(varAuthString)
//Logger.log(" ")
var accept = "application/xml";
var meth = 'POST';
var payloadBlob = testDocBlob("For-Testing-Fax"); //user function returns GAS blob mime type "application/pdf"
var payload = [];
payload = payloadBlob.getBytes();
Logger.log(payload.length)
var contentType = "application/pdf"
var ContentLen = 0;
var headers =
{
"Accept": accept ,
"Authorization": "Basic " + varAuthString
}
//Logger.log("headers= ")
//Logger.log(headers)
//Logger.log(" ")
var options =
{
"method" : meth,
"headers" : headers,
"contentType":contentType,
"contentLength": ContentLen,
"payload" : payload
};
//Logger.log("options= ")
//Logger.log(options)
//Logger.log(" ")
var r = UrlFetchApp.fetch(url,options);
Logger.log(r.getResponseCode());
Logger.log(r.getContentText());
Logger.log(r.getHeaders());
}
You’ve got most of the code you need already, you just need to re-arrange it a bit and build in the multi-part handling.
I suggest you modify your fax sending function to deal with an array of file blobs.
Here’s what the code of your test function would look like, building up the array and calling your
testfax()function:This way, you can set
contentTypedepending on thelengthof the file array. If it’s a single file, takecontentType=fileBlob.getContentType(), otherwisecontentType=multipart/mixed...with an appropriateboundaryparameter.You can choose to use a hard-coded boundary like this – it’s interesting how often the one in this example shows up in searches. Better if you can generate your own random string. This is all described in Section 5.1 of RFC2046, Multipurpose Internet Mail Extensions.
Once your
contentTypeis set, build the payload of your message. If it’s a single file,payloadconsists only offiles[0].getBytes(). Otherwise, loop throughfiles, appending topayloadfor each one.The multipart payload for each file will consist of:
boundarystring prepended by double-hyphens, followed by…content-typetag for the file.files[i].getContentType(), followed by…files[i].getBytes()The multipart payload is then finalized by:
boundarystring enclosed front and back with double-hyphens, signifying the end.The last part of your existing function remains as-is, setting up the
optionswith thepayloadthat has been constructed above, and usingfetch()to send it off. You should now have a function that can handle single or multiple faxes.