I want to have the myFunction() send a payload which will be processed by the doPost and return some content based on the payload. I can only get these test scripts to work in either of these instances:
a) Use doGet(), not pass the payload but get the output returned as expected.
OR
b) Use doPost(), pass the payload but get an 405 code instead of the output.
What am I missing?
function doPost(e) {
var ss = SpreadsheetApp.openById('<spreadsheet id for recording parameters>');
var sheet = ss.getSheets()[0];
var record;
for (var i in e.parameters) {
record = 'parameter: ' + i + ' = ' + e.parameters[i];
sheet.getRange(sheet.getLastRow() + 1, 1, 1, 1).setValue(record);
}
var output = ContentService.createTextOutput();
output.setContent("content to return");
return output;
}
function myFunction() {
var url = '<url of above script webapp, set to anonymous, anyone>';
var payload = {payloadToSend : 'string to send'};
var method = 'post'
var response = UrlFetchApp.fetch(url, {method : method, payload: payload}).getContentText();
Logger.log(response);
return;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…