Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send HEAD request in Google App Script

I am developing a Google App Script to determine the size of a remote resource without downloading it. The code is as follows

function getRemoteFileSize()
{  
  var params =  { "method" : "head" };
  var resp = UrlFetchApp.fetch("https://www.google.com/images/srpr/logo11w.png", params);
  Logger.log("Remote File Size: " + resp.getAllHeaders()["Content-Length"]);
}

However, Google App Script does not seem to support head requests and the code above cannot be executed.

What can be a viable alternative other than issuing a GET request ?
I am open to all suggestions including usage of a third-party service which has an API

like image 263
Extreme Coders Avatar asked Jan 16 '15 05:01

Extreme Coders


People also ask

How do you send a POST request in App script?

The POST API returns a parameter from the query string of the URL and the name from the request body. const doPost = (request = {}) => { const { parameter, postData: { contents, type } = {} } = request; const data = JSON. parse(contents); return ContentService. createTextOutput(parameter.

What is UrlFetchApp?

UrlFetchApp. Fetch resources and communicate with other hosts over the Internet. This service allows scripts to communicate with other applications or access other resources on the web by fetching URLs. A script can use the URL Fetch service to issue HTTP and HTTPS requests and receive responses.

Can I use JavaScript in Google App script?

Google Apps Script is a development platform that makes it fast and easy to create scripts and small applications that integrate with Google Workspace. With Apps Script, you: Write code in JavaScript and access built-in libraries for Google Workspace applications like Gmail, Calendar, Drive, etc.

Does Google App script support ES6?

This syntax includes let , const , and many other popular features. See V8 syntax examples for a short list of popular syntax improvements you can make using the V8 runtime. Caution: ES6 modules are not yet supported.


1 Answers

You can try to override the method by setting the "headers" advanced parameter:

var params = {"method" : "GET", "headers": {"X-HTTP-Method-Override": "HEAD"}};

I've never used "HEAD", so I can't tell you for sure that this will work, but I have used the method override for "PATCH", and had that work.

like image 94
Alan Wells Avatar answered Sep 29 '22 04:09

Alan Wells