使用Google脚本发布JSON数据时状态代码错误

时间:2018-08-11 20:54:29

标签: google-apps-script

我正在编写一个用于发送短信的应用程序脚本。在sms api文档部分中,写有以下内容,可以发送sms:

POST http://clients.muthofun.net/api/v3/sendsms/json 
Host: http://clients.muthofun.net 
Content-Type: application/json 
Accept: */*

{
   "authentication":{
      "username":"test",
      "password":"test"
   },
   "messages":[
      {
         "sender":"044XXXXXXXX",
         "text":"Hello",
         "recipients":[
            {
               "gsm":"88017XXXXXXXX"
            }
         ]
      }
   ]
}

所以我写了以下脚本代码, 修改为@Tanaike所说的

function myFunction() {

    var _auth = {
      "username": "*****", 
      "password": "*****"
    };

    var rec = {
      "gsm": "xxxxxxxxxxx"
    };


    var msg = {
      "sender": "xxxxxxxxxxx", 
      "text": "Hello", 
      "recipients": [rec]
    };

    var payload = {
      "authentication": _auth,
      "messages": [msg]
    };

    _payload = JSON.stringify(payload)


    var options = {
      'method' : 'POST',
      'contentType': 'application/json',
      "accept": "*/*",
      "payload": _payload
    };

    var url = "http://clients.muthofun.net/api/v3/sendsms/json";

    var response = UrlFetchApp.fetch(url, options);

    Logger.log(response);
}

但是实际响应是:

{  
   "results":[  
      {  
         "status":"0",
         "messageid":"10210011344550330860",
         "destination":"88017XXXXXXXX"
      }
   ]
}

但是从Logger函数中,我得到以下响应

{  
   "results":[  
      {  
         "status":"-5",
         "messageid":"",
         "destination":"8801552555645"
      }
   ]
}

是因为我错过了收件人和邮件部分的方括号吗?还是在将发布请求发送到url时出现了问题?

2 个答案:

答案 0 :(得分:0)

此修改如何?在您的示例请求正文中,messages如下。

{
  "authentication": {
    "username": "test",
    "password": "test"
  },
  "messages": [
    {
      "sender": "044XXXXXXXX",
      "text": "Hello",
      "recipients": [
        {
          "gsm": "88017XXXXXXXX"
        }
      ]
    }
  ]
}

在您的脚本中,如下所示。

{
  "authentication": {
    "username": "*****",
    "password": "*****"
  },
  "messages": {
    "sender": "xxxxxxxxxxx",
    "text": "Hello",
    "recipients": {
      "gsm": "xxxxxxxxxxx"
    }
  }
}

在您的脚本中,messagesrecipients的值不是数组。那修改呢?

修改后的脚本:

来自:
var msg = {
  "sender": "xxxxxxxxxxx", 
  "text": "Hello", 
  "recipients": rec
};

var payload = {
  "authentication": _auth,
  "messages": msg
};
至 :
var msg = {
  "sender": "xxxxxxxxxxx", 
  "text": "Hello", 
  "recipients": [rec] // Modified
};

var payload = {
  "authentication": _auth,
  "messages": [msg] // Modified
};

并且accept应该包含在标题中。

我不确定此修改是否可以解决您的问题,因为我无法对其进行测试。如果这对您没有用,您能否提供错误的详细情况?

编辑:

在此修改中,option被修改。

var options = {
  "method" : "POST",
  "contentType": "application/json",
  "headers": {"accept": "*/*"}, // Modified
  "payload": payload // Modified
};

答案 1 :(得分:0)

我没有发出json请求,而是发出了HTTP请求来发送短信。

代码如下:

function myFunction() {
  var username = "*****";
  var password =  "****";

  var msg = "Harry kane didn't score!!! why!!!! why on August!!! :'(";

  var phone = "xxxxxxxxxxx";

  var url = "http://clients.muthofun.com:8901/esmsgw/sendsms.jsp?user="+username+"&password="+password+"&mobiles="+phone+"&sms="+msg;

  Logger.log(url)

  var response = UrlFetchApp.fetch(url);

  Logger.log(response);



}