Google appscript UrlFetchApp won't allow Mongo

2019-09-06 05:42发布

问题:

The following REST API query to the MongoLab database works fine in a browser:

var url = https://api.mongolab.com/api/1/databases/sis/collections/my-coll?q={%22Object%20Type%22:%22Object%20Types%22}&apiKey=

However, when I use Google appscript's UrlFetchApp, as follows:

var oDataObject = JSON.parse(UrlFetchApp.fetch(url).getContentText());

..then appscript returns the following error:

Invalid argument: https://api.mongolab.com/api/1/databases/my-db/collections/my-coll?q=

Does anyone know how to work around this quirk in Google appscript UrlFetchApp?

回答1:

var sQuery = encodeURIComponent(JSON.stringify(oQuery));
var sUrl = 'https://api.mongolab.com/api/1/databases/'
    + MWO_GetDbName() +'/collections/my-    coll?' 
    + 'q=' + sQuery + '&apiKey=' + MWO_GetDbKey(); 
var oDataObject = JSON.parse(UrlFetchApp.fetch(sUrl).getContentText()); 
return  oDataObject;