【问题标题】:GAS: Error when using UrlFetchAppGAS:使用 UrlFetchApp 时出错
【发布时间】:2017-03-12 07:11:03
【问题描述】:

我正在使用senotp serverside api 来验证号码。当它使用具有相同内容的soap ui 运行时,没有错误。但是当我使用UrlFetchApp 执行此操作时,会出现错误:

{"status":"error","response":{"code":"INVALID_REQUEST_BODY"}}

而正确的回答应该是:

{
   "status": "success",
   "response":    {
      "code": "OTP_SENT_SUCCESSFULLY",
      "oneTimePassword": "34859"
   }
}

我使用的代码如下:

var payload =
      {
        "countryCode": "94",
        "mobileNumber": "0766075555",
        "getGeneratedOTP": true
      };

  var header=
      {
        "application-Key":"application_key_value"
      };


  var options =
   {
     "method" : "POST",
     "headers":header,
     "muteHttpExceptions": true,
     "contentType":"application/json",
     "payload" :  payload
   };

  var request=UrlFetchApp.getRequest("https://sendotp.msg91.com/api/generateOTP", options)
  Logger.log(request)
  var response=UrlFetchApp.fetch("https://sendotp.msg91.com/api/generateOTP", options);
  Logger.log(response);

我不确定这里有什么问题。请帮助我调试它或让我知道代码有什么问题。

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    似乎在 GAS fetchService 中使用payload 会自动生成contentType : application/x-www-form-urlencodedcontentType : multipart/form-data

    参见文档:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)

    很遗憾,我还没有找到解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多