The problem I'm running into is that I have a number of API requests I want to make, all of which take several seconds to return because the size of the data being returned, and UrlFetchApp.fetchAll(..)
is simply returning an array of empty JS objects, eg: [{}, {}, {}, ...]
.
My requests array look something like this (formatted for clarity):
requests = [
{
validateHttpsCertificates: false,
url: ';params2=true'
},
{
validateHttpsCertificates: false,
url: ';params4=true'
}
];
The code to make my requests:
responses = UrlFetchApp.fetchAll(requests);
// returns back '[{}, {}]'
console.log(JSON.stringify(responses));
I can confirm through the Database that the API calls are being run, since AWS RDS Performance Metrics show the DB queries run, and I can also confirm that the API itself is responding with 200's via NewRelic, which is why my hunch is that I'm not using GAS/UrlFetchApp.fetchAll()
correctly.
So, I am wondering:
- Does GAS run synchronously, aka, it'll wait for
.fetchAll()
to return before running theconsole.log(...)
line? - Am I actually calling
fetchAll
correctly? Currently at a loss, and the Google Appscript documentation is meager at best.
Thank you in advance for the help.
EDIT:
I migrated to fetchAll
after I successfully used fetch
, eg:
// synchronously fetching one by one
requests.map(request => UrlFetchAll.fetch(request.url, { validateHttpsCertificates: false });
The problem I'm running into is that I have a number of API requests I want to make, all of which take several seconds to return because the size of the data being returned, and UrlFetchApp.fetchAll(..)
is simply returning an array of empty JS objects, eg: [{}, {}, {}, ...]
.
My requests array look something like this (formatted for clarity):
requests = [
{
validateHttpsCertificates: false,
url: 'https://url./api/v2/api_key/endpoint/action?params1=false¶ms2=true'
},
{
validateHttpsCertificates: false,
url: 'https://url./api/v2/api_key/endpoint/action?params3=false¶ms4=true'
}
];
The code to make my requests:
responses = UrlFetchApp.fetchAll(requests);
// returns back '[{}, {}]'
console.log(JSON.stringify(responses));
I can confirm through the Database that the API calls are being run, since AWS RDS Performance Metrics show the DB queries run, and I can also confirm that the API itself is responding with 200's via NewRelic, which is why my hunch is that I'm not using GAS/UrlFetchApp.fetchAll()
correctly.
So, I am wondering:
- Does GAS run synchronously, aka, it'll wait for
.fetchAll()
to return before running theconsole.log(...)
line? - Am I actually calling
fetchAll
correctly? Currently at a loss, and the Google Appscript documentation is meager at best.
Thank you in advance for the help.
EDIT:
I migrated to fetchAll
after I successfully used fetch
, eg:
// synchronously fetching one by one
requests.map(request => UrlFetchAll.fetch(request.url, { validateHttpsCertificates: false });
Share
Improve this question
edited Nov 12, 2019 at 23:28
Stu
asked Nov 12, 2019 at 23:14
StuStu
1093 silver badges11 bronze badges
1 Answer
Reset to default 9How about this answer?
Answer for Question 1:
The fetchAll method works with the asynchronous processing. Ref If you want to use UrlFetchApp with the synchronous processing, please use UrlFetchApp.fetch()
in a loop.
Answer for Question 2:
I think that your request for the fetchAll method is correct. In order to retrieve the responses from UrlFetchApp.fetchAll(requests)
, how about the following modification?
Modified script:
var responses = UrlFetchApp.fetchAll(requests);
var res = responses.map(function(e) {return e.getContentText()});
console.log(JSON.stringify(res)); // or Logger.log(JSON.stringify(res));
- In this modification,
getContentText()
is used for each response. - The order of responses is the same with the order of requests.
References:
- getContentText()
- fetchAll(requests)
- Benchmark: fetchAll method in UrlFetch service for Google Apps Script
If I misunderstood your question and this was not the result you want, I apologize.