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.com/api/v2/api_key/endpoint/action?params1=false¶ms2=true'
},
{
validateHttpsCertificates: false,
url: 'https://url.com/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:
.fetchAll()
to return before running the console.log(...)
line?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 });
How about this answer?
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.
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?
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));
getContentText()
is used for each response.If I misunderstood your question and this was not the result you want, I apologize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With