net/http: improve documentation for Request.Write behavior regarding headers it handles itself #68635
Labels
Documentation
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Go version
go version go1.22.5 darwin/arm64
Output of
go env
in your module/workspace:Not relevant; documentation issue
What did you do?
I attempted to set the
Host
header viaRequest.Header.Set("Host", "www.example.org")
and invoked the request.See this modified playground link (credit to the original here); this just dumps the request info using
httputil.DumpRequestOut
.What did you see happen?
Running the playground link reveals that the host which was actually sent was
www.example.org
, which was set viareq.Host = "www.example.org"
. However, commenting this line out (and notreq.Header.Set("Host", "www.example2.org")
below it) and running it again results in the host becoming1.2.3.4
instead, suggesting that whateverHost
header is set usingHeader.Set
is ignored.What did you expect to see?
I expected
Request.Header.Set("Host", "www.example2.org")
to result in the request'sHost
header becomingwww.example2.org
, especially when it is not explicitly set otherwise.The documentation is not very clear that the value of the
Host
header specifically is one that is ignored byRequest.Write
(well, it's there; just substantially hidden, in my opinion). Here are the steps I took to attempt to find this out starting from https://pkg.go.dev/net/http#Request:Okay, got it; I can use the provided
Request.Host
field to set theHost
header. But still, why doesRequest.Header.Set("Host", "www.example2.org")
not work?Okay, there is a clue that this could indeed be happening with
Host
... let's check outRequest.Write
:Hm, there's nothing here that confirms what was said in the
Header
comments. The closest answer I can find in the documentation is actually right underneathRequest.Write
, in Request.WriteProxy:So, after reading all of that, I guess the
Host
header can only come fromr.Host
andr.URL.Host
. And looking at the source code confirms this. But it really would have been nice if, say,Request.Write
outright stated something along the lines of "The following headers will be overridden: ...". I expected such a sentence since it was hinted at in theRequest.Header
documentation.This is not a new problem by any means; plenty of people have happened upon this behavior in the past (see 1 and 2). I think a documentation change as I suggested would make this more clear.
The text was updated successfully, but these errors were encountered: