Skip to content

Instantly share code, notes, and snippets.

@yehara
Last active April 19, 2019 20:41
Show Gist options
  • Save yehara/9337761 to your computer and use it in GitHub Desktop.
Save yehara/9337761 to your computer and use it in GitHub Desktop.
Google Apps Script で multipart/form-data の HTTP リクエストを送信する
function request() {
var parts = [];
parts.push({name: "processModelInfoId", value: 1});
parts.push({name: "nodeNumber", value: 1});
parts.push({name: "key", value: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"});
parts.push({name: "title", value: "件名をここに"});
parts.push({name: "data[0].upload", value: "ファイル本文をここに", filename: "ファイル名.csv", contentType: "text/csv; charset=UTF-8"});
var boundary = "boundaryboundaryboundaryboundary";
var payload = "";
if (parts.length > 0) {
for (var i=0; i<parts.length; i++) {
payload += "--" + boundary + "\r\n";
if (parts[i].filename !== undefined) {
payload += "Content-Disposition: form-data; name=\"" + parts[i].name + "\"; filename=\"" + parts[i].filename + "\"\r\n";
payload += "Content-Type: " + parts[i].contentType + "\r\n";
payload += "\r\n"
payload += parts[i].value + "\r\n";
} else {
payload += "Content-Disposition: form-data; name=\"" + parts[i].name + "\"\r\n";
payload += "\r\n"
payload += parts[i].value + "\r\n";
}
}
payload += "--" + boundary + "--\r\n"
}
var options = {
"method" : "post",
"contentType" : "multipart/form-data, boundary=" + boundary,
"payload" : payload
};
UrlFetchApp.fetch("https://YYYYYYYYY.questetra.net/System/Event/MessageStart/start", options);
}
@levvsha
Copy link

levvsha commented Apr 19, 2019

Very useful script, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment