« Back to Index

Go: difference between URL host and Request Header Host

View original Gist on GitHub

Tags: #go #http

difference between URL host and Request Header Host.md

The r.URL field is created by parsing the HTTP request URI.

The r.Host field is the value of the Host request header. It’s the same value as calling r.Header.Get("Host").

If the HTTP request on the wire is:

GET /pub/WWW/TheProject.html HTTP/1.1
Host: www.example.org:8080

…then r.URL.Host is "" and r.Host is www.example.org:8080.

The value of r.URL.Host and r.Host are almost always different.

On a proxy server, r.URL.Host is the host of the target server and r.Host is the host of the proxy server itself.

When not connecting through a proxy, the client does not specify a host in the request URI. In this scenario, r.URL.Host is the empty string.

If you are not implementing a proxy, then you should use r.Host to determine the host.