'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.
Solution 1:[1]
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.
Solution 2:[2]
You can send header information with UrlFetchApp in this way:
var url = "http://mymagentohost/api/rest/products?limit=2"
var params = {
headers: { 'Content-Type': "application/json", 'Accept': "application/json",
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'},
muteHttpExceptions: true,
method: "GET",
contentType: "application/json",
validateHttpsCertificates: false,
};
var response = UrlFetchApp.fetch(url, params);
Just add the user agent information in the headers section. Hope it helps!
Credit goes out to: Aditya Advani
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arun Nagarajan |
| Solution 2 |
