GAS: Error 500 while connecting to an API

允我心安 提交于 2020-06-29 04:13:27

问题


I'm trying to connect to Cloud Waitress API, a solution for restaurants,

Documentation: https://apidocs.cloudwaitress.com/#orderpromos

It's documentation gives an example on how to connect to the API:

curl https://api.cloudwaitress.com/v1/orders \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: YOUR_API_KEY" \ 
  -d `
    {
      "restaurant_id": "xxxxxxx",
      "limit": 10,
      "page": 1,
      "sort": { "created": -1 },
    }
  `

I tried to create a Script in GAS to get the information back to a Spreadsheet.

This is my code so far:

function GetOrders(){  
  var url='https://api.cloudwaitress.com/v1/orders';
  var options = {
    "method": "POST",
    "headers":{
      "Content-Type": "application/json",
      "Authorization": "SOME-AUTHORIZATION-KEY"
    },
    "payload": {
      "restaurant_id":"SOME-RESTAURANT-ID",
      "limit": 10,
      "page": 1,
      "sort": { "created": -1 }
    }
    /*
    ,
    "muteHttpExceptions": true
    */
  };

  var response = UrlFetchApp.fetch(url, options);
  var data = JSON.parse(response.getContentText());


  var TotalRecord = data["outcome"];
  Logger.log("response: " + response);
  Logger.log("data: " + data);
  Logger.log("TotalRecord: " + TotalRecord);
}

However I'm getting a 500 error

Exception: Request failed for https://api.cloudwaitress.com returned code 500. Truncated server response: {"error":"Internal Error"} (use muteHttpExceptions option to examine full response) (line 23, file "Code")

According to their website a 500 error corresponds to:

I would interpretate the error as if something is wrong of their side, which I doubt.

However, seeing my code I can't figure out what might be missing.

Additional notes:

Output when Mutting exceptions:

Thanks for your time.

来源:https://stackoverflow.com/questions/62135141/gas-error-500-while-connecting-to-an-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!