Problem Using UrlFetchApp with Long URLs

326 views
Skip to first unread message

Davis Jones

unread,
Aug 5, 2019, 11:27:37 AM8/5/19
to Google Apps Script Community

Hello fellow GAS devs,


I'm using the UrlFetchApp to call this neat GrammarBot API. The API itself accepts strings that are up to 50,000 characters in length, but it seems that the UrlFetchApp method greatly limits the possibilities of using this API. 


When using GrammarBot, I'm able to successfully make a call with the following code:

function grammarBotTest () {
  var rawText = 'This is a free online calculator which counts the number of characters or letters in a text, useful for your tweets on Twitter, as well as a multitude of other applications.';
  var encodedText = encodeURI(rawText);
  var url = 'http://api.grammarbot.io/v2/check?api_key=AF5B9M2X&text=' + encodedText + '&language=en-US';
  
  // var url = 'http://api.grammarbot.io/v2/check?api_key=AF5B9M2X&text=I%20can%27t%20remember%20how%20to%20go%20their.&language=en-US';
  var params = {
    'contentType': 'application/json',
    'method': 'get',
    'headers': {
      'api-key': '###'
    },
    'muteHttpExceptions': true
  }
  var response = UrlFetchApp.fetch(url, params);
  Logger.log(response); // Logger.log() replaces console.log() in Google Apps Script
}

The problem is that, when the rawText variable at the top of the function is a text string of a more useful size (for example, we are trying to help students with their resumes, and this involves around 7000 characters usually), the UrlFetch method will reject the url provided as an argument to the method because the URL itself is too long. After reviewing a few threads on Stack Overflow, I see that the UrlFetchApp will reject URLs as arguments that are ~2000 characters or more.


If you can help me troubleshoot this problem or propose an alternative method of calling this API, I'd be grateful!

Martin Hawksey

unread,
Aug 5, 2019, 12:35:57 PM8/5/19
to Google Apps Script Community
I'm guessing their API requires parameters in the URL rather than using a POST request body? This would be a way to get around Google Apps Script URL length limit. Perhaps they would be willing to add it as a feature of not available?

Eric Koleda

unread,
Aug 13, 2019, 9:43:00 AM8/13/19
to Google Apps Script Community
Although the API docs don't mention POST, the Node.js client they provide uses it:


- Eric
Reply all
Reply to author
Forward
0 new messages