title: How to use ContentService and doPost to create a REST API
body: <p>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:</p>
  
  <p>a) Use doGet(), not pass the payload but get the output returned as expected.</p>
  
  <p>OR</p>
  
  <p>b) Use doPost(), pass the payload but get an 405 code instead of the output.</p>
  
  <p>What am I missing?</p>
  
  <pre><code>function doPost(e) {
    var ss = SpreadsheetApp.openById('&lt;spreadsheet id for recording parameters&gt;');
    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 = '&lt;url of above script webapp, set to anonymous, anyone&gt;';
    var payload = {payloadToSend : 'string to send'};
    var method = 'post'
    var response = UrlFetchApp.fetch(url, {method : method, payload: payload}).getContentText();
    Logger.log(response);
    return;
  }
  </code></pre>