« Back to Index

Go: r.Host vs r.URL.Host

View original Gist on GitHub

Tags: #go #http

r.Host vs r.URL.Host.md

For clarity on r.Host vs r.URL.Host see:
https://stackoverflow.com/a/42926149/14849316

But in summary:

GET https://www.google.com/?q=xxx HTTP/1.1
Host: [gg.proxy.com](http://gg.proxy.com/)

Will set r.URL.Host to www.google.com and Host to gg.proxy.com.

While for GET /?q=xxx HTTP/1.1 the r.URL.Host will be empty.

UPDATE go 1.17

// RFC 7230, section 5.3: Must treat
//      GET /index.html HTTP/1.1
//      Host: www.google.com
// and
//      GET http://www.google.com/index.html HTTP/1.1
//      Host: doesntmatter
// the same. In the second case, any Host line is ignored.
req.Host = req.URL.Host