Error from UrlFetchApp when passing Short URL

442 views Asked by At

Facing below Exception while calling get request using UrlFetchApp.fetch.

Exception: Request failed for https://shorturl.com (myshorturl) returned code 302

I do not get the error when longurl length is 5606 length. I get the error when longurl length is more than 5609 length.

 var url = shortenUrl(longurl);
 Logger.log("URL:" + url);
 var response = UrlFetchApp.fetch(url, {
     "method": "get",followRedirects: true
 });
 Logger.log("Response" + response.getContentText())


function shortenUrl(longURL) {

var url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=mykey";

var payload = {
    "dynamicLinkInfo": {
        "dynamicLinkDomain": "mypage.page.link",
        "link": longURL,
        "iosInfo": {
            "iosBundleId": "mybundleId"
        }
    }
};

var parameters = {
    method: 'post',
    payload: JSON.stringify(payload),
    contentType: 'application/json',
    muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, parameters);
var myArr = JSON.parse(response);
var shortLink = myArr['shortLink'];
return shortLink;
}
1

There are 1 answers

1
TheMaster On

The shortUrl will redirect to another url. 302 Found status code informs the client that there is a redirection and provides the redirect url. Try setting the following parameter in addition:

followRedirects: true,    
muteHttpExceptions: true

EDIT:
Based on the new information, that the longUrl length exceeds 5000 characters, Note that UrlFetch calls have hard limitations on the url length, which is currently 2kB/call. If we assume 1byte per url character, the maximum advertised acceptable url length is around 2048 characters.

References: