Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script POST request UrlFetchApp

I am attempting to Add Groups & 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 & 405 errors depending upon how I prepare the data.

This gets a 400 error:

  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&api_key=KEYWORKSFORGETREQEUSTS";
  var response = UrlFetchApp.fetch(url, options);

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 here

I got it working shortly after this post. Here is what I did:

  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&api_key=GOODAPIKEY";
  var response = UrlFetchApp.fetch(url, options);
like image 437
jcvd Avatar asked Aug 21 '13 20:08

jcvd


People also ask

What is UrlFetchApp?

UrlFetchApp. Fetch resources and communicate with other hosts over the Internet. This service allows scripts to communicate with other applications or access other resources on the web by fetching URLs. A script can use the URL Fetch service to issue HTTP and HTTPS requests and receive responses.

How do I handle GET and POST HTTP requests in Google Apps Script?

Handle POST Requests with Google ScriptsThe callback function doPost is invoked when an HTTP POST request is make to your Google Script URL that is published as a web app with anonymous access. const doPost = (request) => { console. log(request); return ContentService. crateTextOutput(JSON.

Is UrlFetchApp synchronous?

UrlFetchApp methods are synchronous. They return an HttpResponse object, and they do not take a callback.

How do you concatenate in a Google app script?

To concatenate two strings (i.e., to join them together), use the concatenation operator ( + ). Both the concatenation operator and the addition operator have the same symbol + but Apps Script will figure out which operation to perform based on the values being operated on.


2 Answers

I placed the answer in the edit portion of my question. I am not sure which of the three changes I made that made it work, but I left example code that did work.

 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&api_key=GOODAPIKEY";
  var response = UrlFetchApp.fetch(url, options);
like image 171
jcvd Avatar answered Oct 23 '22 04:10

jcvd


The solution you posted didn't work for me, this works for me:

 function example() {

   var payload = {
     "api_key" : "SFS1234SDF",
     "list_id" : "4"
   };

   var options = {
     "method"  : "post",
     "payload" : payload
   };

   var url        = "http://my-url.com";   
   var response   = UrlFetchApp.fetch(url, options);   

   Logger.log(response);

   return response.getContentText();

 }

Use the View -> Log option for debugging.

like image 31
a20 Avatar answered Oct 23 '22 02:10

a20