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);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…