我正在尝试添加群组&使用GoogleApps脚本中的API与SendHub帐户建立联系。我可以成功地发出GET请求并重新获取JSON数据。当我尝试POST请求添加对象时,我收到400,401& 405错误取决于我如何准备数据。
这会出现400错误:
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);
我已经做了几次不同的尝试来改变选项对象的形成方式,结果因400,401或405错误而异。我对如何为here
的SendHub API形成正确的POST请求感到困惑我在这篇文章后不久就开始工作了。这是我做的:
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);
答案 0 :(得分:2)
我把答案放在我问题的编辑部分。我不确定我做出的三个更改中的哪一个使它工作,但我留下了可行的示例代码。
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);
答案 1 :(得分:1)
您发布的解决方案对我不起作用,这对我有用:
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();
}
使用视图 - >用于调试的日志选项。