Re: net/http requests seem to ignore Host headers?

957 views
Skip to first unread message

Kamil Kisiel

unread,
Oct 30, 2012, 1:33:03 PM10/30/12
to golan...@googlegroups.com
The request type has a Host field which sets the Host header. Otherwise it's taken from url.Host.

On Tuesday, October 30, 2012 9:14:38 AM UTC-7, Michael Gorsuch wrote:
Hey all -

I'm working to build a simple tool that allows one to check a given url and emit simple performance metrics.  One feature of this tool is that I should be able to visit a url such as "http://1.2.3.4" and apply a separate Host header to it such as "example.org".  This would allow one to examine hosts that are capable of serving a specific URL but not necessarily resolving.  This is similar to what one could do with curl via the "-H" flag.

However, it appears that requests don't actually allow the Host header to take precedence.

An example:


**
package main

import "os"
import "net/http"

func main() {
req, _ := http.NewRequest("GET", "http://1.2.3.4", nil)
// I wish explicitly setting this Host header would take precedence
req.Header.Set("Host", "www.example.org")
req.Write(os.Stdout)
}
**

Example output:

GET / HTTP/1.1

Host: 1.2.3.4

User-Agent: Go http package

Before I go further, am I doing this correctly?  Is there another way?

Best,

Michael Gorsuch

Michael Gorsuch

unread,
Oct 30, 2012, 1:55:17 PM10/30/12
to Kamil Kisiel, golan...@googlegroups.com
Ah yes.  You are correct.  I noticed Host earlier, but could've sworn that my experiments were showing that setting Host was actually altering the destination host along with the header.  Things seems to be working now, so clearly I was mistaken.

Thank you for your help!

Best,

Michael

--
 
 

Dustin

unread,
Oct 30, 2012, 2:02:32 PM10/30/12
to golan...@googlegroups.com, Kamil Kisiel

On Tuesday, October 30, 2012 10:55:25 AM UTC-7, Michael Gorsuch wrote:
Ah yes.  You are correct.  I noticed Host earlier, but could've sworn that my experiments were showing that setting Host was actually altering the destination host along with the header.  Things seems to be working now, so clearly I was mistaken.

  url.Host will do that.  It's slightly confusing since there are three potential places to specify it.
Reply all
Reply to author
Forward
0 new messages