how to use google apps scripts forcing to send http header format including 'Host':'www.XXX.net'

30 views
Skip to first unread message

cron gas

unread,
Jun 17, 2026, 2:58:01 AM (7 days ago) Jun 17
to Google Apps Script Community

I use UrlFetchApp send  http  Header  including 'Host':'www.XXX.net'

but it  happen "Attribute provided with invalid value: Header:Host" error,

because i use http interceptor recopy Header of  packet that connect 200 OK: HTTP

this  200 OK: HTTP Header  must include 'Host':'www.XXX.net'  in the send packet to the server

if exclude 'Host':'www.XXX.net'  ,my request UrlFetchApp is invalid( HTTP 400 (Bad Request))

I  seach some knowledge about google apps script  news:

The UrlFetchApp service does not allow manual setting of the Host request header packet, but GAS will automatically generate and set the Host header based on the URL you request.

can i give up UrlFetchApp of google apps script  ? can i find the other method to work around~

i test the serverless free web for example:  cron-job.org  and send  http  Header including 'Host':'www.XXX.net'

my request UrlFetchApp is  invalid( HTTP 400 (Bad Request)) too . thanks~

Kildere S Irineu

unread,
Jun 18, 2026, 7:37:47 AM (5 days ago) Jun 18
to Google Apps Script Community

Yes — in Google Apps Script UrlFetchApp you cannot force/set the Host header manually. Host is generated automatically from the URL by Google’s HTTP infrastructure. Trying this:

headers: {
Host: 'www.XXX.net'
}

causes:

Attribute provided with invalid value: Header:Host

UrlFetchApp is designed to fetch external URLs through Google infrastructure and supports normal request options, headers, payload, etc., but not low-level control of protected transport headers like Host. It also requires the external request scope.

Use one of these workarounds:

function callApi() {
const url = 'https://www.XXX.net/path';

const options = {
method: 'get',
headers: {
'User-Agent': 'Mozilla/5.0',
'Accept': 'application/json'
// Do NOT add Host here
},
muteHttpExceptions: true
};

const response = UrlFetchApp.fetch(url, options);
console.log(response.getResponseCode());
console.log(response.getContentText());
}

If the server requires Host: www.XXX.net, then the URL itself must be:

If you need to connect to an IP address or another domain while sending Host: www.XXX.net, Apps Script cannot do that directly.

Best workaround: create a small proxy outside Apps Script, for example Cloudflare Worker, Google Cloud Run, AWS Lambda, VPS, etc. Apps Script calls the proxy, and the proxy sends the raw HTTP request with the custom Host.

Example flow:

Apps Script → Proxy server → Target server with custom Host header

So, no need to “give up” Apps Script entirely. Use Apps Script for scheduling/cron and data handling, but use an external proxy for the HTTP request that needs custom Host.

Also check with the server owner: many 400 Bad Request cases happen because the server expects the correct domain/SNI/TLS config, not just the Host header.


Systems Analyst & Developer - AI
Business Administrator — MBA in Management
Reply all
Reply to author
Forward
0 new messages