简体   繁体   中英

Force value of 'Host' header in Angular $http request

I want to access an API site hosted on a multi-site server S. I have the ip adress of S, but there aren't DNS servers, so I have to make a http request with the following parameters :

{
  URL : 'http://192.168.1.xxx:80/data',
  headers : {
    Host : 'api.somesite.com'
  },
  data : ...
}

However, the 'Host' header is automatically replaced by Angular to '192.168.1.xxx' before sending (and thus the request fails). I already tried to implement a http interceptor :

api.run(['$http',function($http) {
    console.log("set host");
    $http.defaults.headers.common.Host = "api.somesite.com";
}]);

...without results : the server answers with an ERROR 500, and Development Tools show that the request had the header 'Host' set to the ip address.

Alternatively, are there other ways to make an http request to a Website by using its ip (without DNS)?

Host is defined a forbidden header name in the XMLHttpRequest / fetch specifications so it is impossible to set it from JavaScript in the browser.

Sorting out DNS, editing the system hosts file on a per-client basis, or configuring each client to use an HTTP proxy that knows how to resolve the URL are the only ways you can solve this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM