Google Apps脚本POST请求UrlFetchApp [英] Google Apps Script POST request UrlFetchApp

查看:132
本文介绍了Google Apps脚本POST请求UrlFetchApp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加组&使用GoogleApps脚本中的API与SendHub帐户联系的联系人.我可以成功发出GET请求,并毫无问题地获得JSON数据.当我尝试POST请求添加对象时,我收到了400、401& 405错误取决于我如何准备数据.

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.

这将显示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错误变化而来.我对如何为SendHub API形成正确的POST请求感到困惑,该请求位于此处

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);

推荐答案

我将答案放在问题的编辑部分中.我不确定我所做的三个更改中的哪一个使它起作用了,但是我留下了有效的示例代码.

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);

这篇关于Google Apps脚本POST请求UrlFetchApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆