Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set the Host header using fetch API

I have a reverse proxy server, which redirects you to different services depending on the Host header. However when making requests to this server using a browser, the Host is always set to the domain name in the URL. I tried:

fetch("http://foo.com", {"headers":{"Host":"bar.foo.com"}})

But it doesn't work

like image 678
hgiesel Avatar asked Apr 07 '17 18:04

hgiesel


People also ask

What is header in fetch API?

The Headers interface of the Fetch API allows you to perform various actions on HTTP request and response headers. These actions include retrieving, setting, adding to, and removing headers from the list of the request's headers.

What is host header API?

The Host request header specifies the host and port number of the server to which the request is being sent. If no port is included, the default port for the service requested is implied (e.g., 443 for an HTTPS URL, and 80 for an HTTP URL). A Host header field must be sent in all HTTP/1.1 request messages.


2 Answers

Host is one of the forbidden header names:

A forbidden header name is an HTTP header name that cannot be modified programmatically.

like image 144
robertklep Avatar answered Oct 19 '22 13:10

robertklep


It won't work. You cannot set the forbidden Headers on making the requests through browsers.

You can get the list of forbidden headers here - https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name

Similar answers here:
Ajax request: Refused to set unsafe header
Not able to set HTTP Host header on $.ajax request

like image 2
Anurag Avatar answered Oct 19 '22 12:10

Anurag