Google Apps 脚本 - 使用日期参数从第三方 API 获取数据
Google Apps Script - Fetch data from third party API with date parameter
我的一个供应商创建了一个 API,我可以用它来提取数据。我正在尝试为此使用 google 应用程序脚本,因为我可以轻松地安排数据提取。
我以前使用过 UrlFetchApp,但我迷路了,因为我必须在其中传递一个额外的日期参数 - 我不知道该怎么做,非常感谢你帮助解决这个问题出。
给我的指示是:
url = "https://xxxxx.com/api/leads/"
method = POST
//Request JSON format: (date format should be ‘Y-m-d’)
{"date":"2019-08-21"}
除了谷歌搜索之外,我还尝试了各种迭代,将日期参数添加到 UrlFetchApp 的选项中,并尝试将其附加到 URL 字符串中。
这是我目前拥有的:
function myFunction() {
var pullDate = new Date().toISOString().split('T')[0];
var dateParams = {"date": pullDate};
var url = "https://xxxxx.com/api/leads/";
var options = {
"method" : "POST",
"contentType" : "application/json"
}
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
这是我收到的回复:
[19-09-09 12:44:36:586 IST] {"result":"date is empty!"}
预期的响应是这样的:
{"result":[
{"count":"55","model":"ABC","date":"2019-09-09"},
{"count":"2","model":"DEF","date":"2019-09-09"},
{"count":"48","model":"XYZ","date":"2019-09-09"}
]}
非常感谢你帮助我解决这个问题!
问题:
payload
(或请求正文)从请求中丢失。
片段:
- 您可以在
options
中包含 payload
:
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload": JSON.stringify(dateParams)
}
我的一个供应商创建了一个 API,我可以用它来提取数据。我正在尝试为此使用 google 应用程序脚本,因为我可以轻松地安排数据提取。
我以前使用过 UrlFetchApp,但我迷路了,因为我必须在其中传递一个额外的日期参数 - 我不知道该怎么做,非常感谢你帮助解决这个问题出。
给我的指示是:
url = "https://xxxxx.com/api/leads/"
method = POST
//Request JSON format: (date format should be ‘Y-m-d’)
{"date":"2019-08-21"}
除了谷歌搜索之外,我还尝试了各种迭代,将日期参数添加到 UrlFetchApp 的选项中,并尝试将其附加到 URL 字符串中。
这是我目前拥有的:
function myFunction() {
var pullDate = new Date().toISOString().split('T')[0];
var dateParams = {"date": pullDate};
var url = "https://xxxxx.com/api/leads/";
var options = {
"method" : "POST",
"contentType" : "application/json"
}
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
这是我收到的回复:
[19-09-09 12:44:36:586 IST] {"result":"date is empty!"}
预期的响应是这样的:
{"result":[
{"count":"55","model":"ABC","date":"2019-09-09"},
{"count":"2","model":"DEF","date":"2019-09-09"},
{"count":"48","model":"XYZ","date":"2019-09-09"}
]}
非常感谢你帮助我解决这个问题!
问题:
payload
(或请求正文)从请求中丢失。
片段:
- 您可以在
options
中包含payload
:
var options = {
"method" : "POST",
"contentType" : "application/json",
"payload": JSON.stringify(dateParams)
}