0

I have a bit of GAS code that is failing when executing UrlFetchApp.fetch(url) - I think.

Are there limits to the character length of a url when used in UrlFetchApp.fetch(url). My function is failing and I suspect that it has something to do with the length of the url. It is over 100 chars.

The code below refers...

function uploadToDrive(url, folderid, filename, fileDesc) {
  var msg = '';

  try {
    var response = UrlFetchApp.fetch(url);

  } catch(err) {

  };

  if (response.getResponseCode() === 200) {

       var folder = DriveApp.getRootFolder();
      if (folderid) {
        folder = DriveApp.getFolderById(folderid);
      }
      var blob = response.getBlob();
      var file = folder.createFile(blob);
      file.setName(filename);

      file.setDescription(fileDesc); 

      var headers = response.getHeaders();
      var content_length = NaN;
      for (var key in headers) {
        if (key.toLowerCase() == 'Content-Length'.toLowerCase()) {
          content_length = parseInt(headers[key], 10);
          break;
        }
      }

      var blob_length = blob.getBytes().length;
      msg += 'Saved "' + filename + '" (' + blob_length + ' bytes)';
      if (!isNaN(content_length)) {
        if (blob_length < content_length) {
          msg += ' WARNING: truncated from ' + content_length + ' bytes.';
        } else if (blob_length > content_length) {
          msg += ' WARNING: size is greater than expected ' + content_length + ' bytes from Content-Length header.';
        }
      }
      msg += '\nto folder "' + folder.getName() + '".\n';
    }
   else {
    msg += 'Response code: ' + response.getResponseCode() + '\n';
  }

  return file.getUrl();
}
Mike Eburne
  • 351
  • 2
  • 7
  • 21
  • Is this information useful for your situation? https://stackoverflow.com/q/2659952/7108653 By the way, in order to correctly understand about `fails` you say, can you provide the error message and the error line of your script? – Tanaike Oct 29 '19 at 07:41
  • Line 5 - var response = UrlFetchApp.fetch(url) - Returns error code 404. – Mike Eburne Oct 29 '19 at 08:24
  • I tried the method with a 300+ character long url and it worked. The problem might be related to this specific url. Could you by any chance share this url? – Iamblichus Oct 29 '19 at 09:16
  • Here is is: https://www.jotform.com/uploads/Administrator_System_sysadmin/92960977977584/4480552280228238115/BoE%20Test%20File.docx – Mike Eburne Oct 29 '19 at 09:24
  • This url returns a 404. This problem has no relation to GAS limits. – Iamblichus Oct 29 '19 at 15:27

1 Answers1

1

That link generates a response code 404, but you set the response variable only if the fetch method is successful in your try block. Try validating the response variable before assuming it has properties and methods to access:

function fetching() {
  var url = "<some url resource>";

  try {
    var response = UrlFetchApp.fetch(url);
  } catch (err) {
// why catch the error if you aren't going to do anything with it?
    Logger.log(err);
  } finally {
    if (response && response.getResponseCode() === 200) {
      Logger.log("Should be 200, got " + response.getResponseCode());
    } else {
      Logger.log("Fetching Failed: Exception thrown, no response.");
    }
  }
}

However, I would go farther and guarantee a response from UrlFetchApp.fetch. This is done with the "muteHttpExceptions" paramater set to true.

function fetching() {
  //dns exception
  //var = "googqdoiqijwdle.com"
  //200
  var url = "google.com";
  //404
  //var url = "https://www.jotform.com/uploads/Administrator_System_sysadmin/92960977977584/4480552280228238115/BoE%20Test%20File.docx"

  try {
    var response = UrlFetchApp.fetch(url, {
      muteHttpExceptions: true
    });
  } catch (err) {
    Logger.log(err);
  }

  if (response) {
    response.getResponseCode() === 200 ?
      Logger.log("Should be 200, got " + response.getResponseCode()) :
      Logger.log("Anything but 200, got " + response.getResponseCode());
  }
}

Hope this helped!

Docs: Google Developers

blrzzzt
  • 194
  • 5