Javascript Google脚本API routexl错误409

Javascript Google脚本API routexl错误409,javascript,json,google-apps-script,google-apps,Javascript,Json,Google Apps Script,Google Apps,我正在尝试制作一个脚本,在routexl API的帮助下,它可以计算出通过多个位置的最快路径。然而,我尝试的每件事似乎都出现了错误409截断服务器响应。我的代码如下 function myFunctionpost() { var url = "https://api.routexl.nl/distances"; var places = [{"address":"1","lat":"52.05429","lng":"4.248618"},{"address":"2","

我正在尝试制作一个脚本,在routexl API的帮助下,它可以计算出通过多个位置的最快路径。然而,我尝试的每件事似乎都出现了错误409截断服务器响应。我的代码如下

 function myFunctionpost() {
      var url = "https://api.routexl.nl/distances";
      var places = [{"address":"1","lat":"52.05429","lng":"4.248618"},{"address":"2","lat":"52.076892","lng":"4.26975"},{"address":"3","lat":"51.669946","lng":"5.61852"}];
      var locations = JSON.stringify(places);
      var headers = { 
          "Authorization":"Basic " + Utilities.base64Encode("Username:password"),
          "Content-Type":"application/json"
      };
      var options = { 
          'method' : 'post',
          'headers' : headers,
          'contentType': 'application/json',
          'payload' : locations,
          'muteHttpExceptions' : true
      };
      var response = UrlFetchApp.fetch(url, options);
      //var json = response.getContentText();
      //var data = JSON.parse(json);
      Logger.log(response);
  }

409错误表示已找到。我不是谷歌应用程序专家,但RoutexlAPI期望“位置”作为输入参数。在您的示例中,locations JSON是在没有locations键的情况下发布的

更新:在对Google Apps脚本进行一些测试之后,我还发现ContentType需要调整

下面是一个对我有用的例子:

function myFunctionpost() {
      var url = "https://api.routexl.nl/distances";
      var places = [{"address":"1","lat":"52.05429","lng":"4.248618"},{"address":"2","lat":"52.076892","lng":"4.26975"},{"address":"3","lat":"51.669946","lng":"5.61852"}];
      var locations = JSON.stringify(places);
      var headers = { 
          "Authorization":"Basic " + Utilities.base64Encode("username:password")
      };
      var options = { 
          'method' : 'post',
          'headers' : headers,
          'contentType': 'application/x-www-form-urlencoded',
          'payload' : {'locations': locations},
          'muteHttpExceptions' : true
      };
      var response = UrlFetchApp.fetch(url, options);
      //var json = response.getContentText();
      //var data = JSON.parse(json);
      Logger.log(response);
  }

409错误表示已找到。我不是谷歌应用程序专家,但RoutexlAPI期望“位置”作为输入参数。在您的示例中,locations JSON是在没有locations键的情况下发布的

更新:在对Google Apps脚本进行一些测试之后,我还发现ContentType需要调整

下面是一个对我有用的例子:

function myFunctionpost() {
      var url = "https://api.routexl.nl/distances";
      var places = [{"address":"1","lat":"52.05429","lng":"4.248618"},{"address":"2","lat":"52.076892","lng":"4.26975"},{"address":"3","lat":"51.669946","lng":"5.61852"}];
      var locations = JSON.stringify(places);
      var headers = { 
          "Authorization":"Basic " + Utilities.base64Encode("username:password")
      };
      var options = { 
          'method' : 'post',
          'headers' : headers,
          'contentType': 'application/x-www-form-urlencoded',
          'payload' : {'locations': locations},
          'muteHttpExceptions' : true
      };
      var response = UrlFetchApp.fetch(url, options);
      //var json = response.getContentText();
      //var data = JSON.parse(json);
      Logger.log(response);
  }

muteHttpExceptions:true
添加到选项中。409可能意味着服务不喜欢您的有效负载。如果启用muteHttpExceptions,则响应中可能会有更多数据。感谢@SpenceEaston我按照您的建议添加了此项,这给了我另一个问题“语法错误,意外标记:N”,在“var data=JSON.parse(JSON);”行上,因此,我只是暂时将其注释掉以记录响应。日志中提供的新信息是“未找到输入”。将
muteHttpExceptions:true
添加到选项中。409可能意味着服务不喜欢您的有效负载。如果启用muteHttpExceptions,则响应中可能会有更多数据。感谢@SpenceEaston我按照您的建议添加了此项,这给了我另一个问题“语法错误,意外标记:N”,在“var data=JSON.parse(JSON);”行上,因此,我只是暂时将其注释掉以记录响应。日志中提供的新信息是“找不到输入”。只需将有效负载值JSON.STRING化就可以了。@SpenceEaston不是已经有了吗?var locations=JSON.stringify(位置);我报名测试这个。我已经尝试了发布数据的各种组合,但我无法让它发挥作用。我甚至在使用API文档中的示例数据……我可以点击状态页,但距离端点不会接受任何形式的post数据。@SpenceEaston感谢您的测试。当我进入GoogleApps脚本并在ContentType中发现问题时,我会相应地更新我的答案。啊,这很有意义。应更新API文档以显示这一点。它们并不具体,但给人的印象是使用JSON编码只是需要JSON.stringify有效负载值,这应该可以做到。@SpenceEaston不是已经做到了吗?var locations=JSON.stringify(位置);我报名测试这个。我已经尝试了发布数据的各种组合,但我无法让它发挥作用。我甚至在使用API文档中的示例数据……我可以点击状态页,但距离端点不会接受任何形式的post数据。@SpenceEaston感谢您的测试。当我进入GoogleApps脚本并在ContentType中发现问题时,我会相应地更新我的答案。啊,这很有意义。应更新API文档以显示这一点。它们并不具体,但给人的印象是使用JSON编码