Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlFetch with custom user-agent string?

Is it possible to change the user-agent string used with Google Apps Script UrlFetchApp.fetch requests?

This discussion from 2010 insinuates that the UrlFetch module in Google Apps Script supports adding the User-Agent header to the optional headers collection, like the UrlFetch module of Google App Engine does. However, the GAS documentation states nothing about this. A test script I made also shows it doesn't work.

Test script:

function testUserAgentString(){
  var page;
  try {
    page = UrlFetchApp.fetch('http://www.myuseragent.net/',
        {headers: {"User-Agent":
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"
        }}).getContentText();
    Logger.log(page);
  } catch(_) {}
}

Log output (minus irrelevant HTML) was:

Mozilla/5.0 (compatible; GoogleDocs; script; +http://docs.google.com)

which is the default user-agent string used when doing UrlFetch requests by GAS.

Am I missing something or doing something wrong?

Edit: As this is currently not possible, I placed an enhancement request on the Issue Tracker, per Arun's suggestion.

like image 937
Protector one Avatar asked Feb 25 '13 12:02

Protector one


People also ask

What is UrlFetchApp Fetch()?

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.

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 a URL fetch call?

URL Fetch. This service allows scripts to access other resources on the web by fetching URLs. A script can use the UrlFetch service to issue HTTP and HTTPS requests and receive responses. The UrlFetch service uses Google's network infrastructure for efficiency and scaling purposes.


1 Answers

This is not possible today. Please log an enhancement request on the Issue Tracker with your use cases so this can be reviewed and considered.

like image 98
Arun Nagarajan Avatar answered Sep 21 '22 13:09

Arun Nagarajan