title: Google Apps Script POST request UrlFetchApp
body: <p>I am attempting to Add Groups &amp; Contacts to a SendHub account using their API from GoogleApps Script.  I can successfully make GET requests and get JSON data back with no issues.  When I attempt the POST request to add objects I have received 400, 401 &amp; 405  errors depending upon how I prepare the data.</p>
  
  <p>This gets a 400 error:</p>
  
  <pre><code>  var headers = {
      "Content-type" : "application/json"
    };
    var data = {
      "name" : "Me Testing",
      "slug" : "me-testing",
      "text_to_subscribe" : true
    };
    var payload = {
      "data" : Utilities.jsonStringify(data)
    };
    var options = {
      "method" : "post",
      "headers" : headers,
      "payload" : payload
    };
    var url = "https://api.sendhub.com/v1/groups/?username=USERNAMEWORKSFORGETREQUESTS&amp;api_key=KEYWORKSFORGETREQEUSTS";
    var response = UrlFetchApp.fetch(url, options);
  </code></pre>
  
  <p>I have made several different attempts in changing the way the options object is formed and the results vary from 400, 401 or 405 errors.  I am confused on how to form the proper POST request for the SendHub API which is <a href="http://apidocs.sendhub.com/Groups.html" rel="noreferrer">here</a></p>
  
  <p>I got it working shortly after this post.  Here is what I did:</p>
  
  <pre><code>  var data = {
      "name" : "Me Testing",
      "slug" : "me-testing",
      "text_to_subscribe" : "true"
    };
    var payload = JSON.stringify(data);
    var options = {
      "method" : "POST",
      "contentType" : "application/json",
      "payload" : payload
    };
    var url = "https://api.sendhub.com/v1/groups/?username=GOODUSERNAME&amp;api_key=GOODAPIKEY";
    var response = UrlFetchApp.fetch(url, options);
  </code></pre>