This is a creation in Article, where the information may have evolved or changed.
Objective
I have been studying and writing Ruby and go in recent time, especially go, as the standard language of the cloud computing era, write up still quite feel, sad it will become more and more fire.
However, in the process of writing, also encountered a few small problems, this article is to share about the Go language set HTTP request in the Host header a small consideration.
General Practice
Usually we do this when we set the header request for http:
Header.Add("Authentization", "TOKEN")Header.Add("Content-Type", "application/json")...
Java, Ruby, Go are so, the difference is only the syntax is different. However, the handling of the host header is different. In go, if we write this:
header.Add("Host", "XXXXXXXXX")
Then the problem comes out--maybe you don't see any errors in the log sent by the request, but if you look at the log on the server, you'll see that the server receives the host header not what you want to send, but still the host in the URL.
What is this for?
Why does the host header in go not add up to this?
The design of Go is to define this request property with a separate host property, referring to the request struct defined by the Request.go file in the Net/http package:
// For server requests Host specifies the host on which the// URL is sought. Per RFC 2616, this is either the value of// the "Host" header or the host name given in the URL itself.// It may be of the form "host:port".//// For client requests Host optionally overrides the Host// header to send. If empty, the Request.Write method uses// the value of URL.Host.Host string
So in use, if we want to pass a specific host, we should do this:
Req.Host="XXXX"
But why is go so special?
Why is go so special?
Originally from the HTTP spec angle, is not likely to let the host header can be modified, here the host should be obtained from the URL, rather than arbitrary designation.
But considering a lot of scenarios, especially the client requests, many times we want to be able to modify this host parameter to simulate our needs, if go can respond to modify its source code is good?
Search, found that the problem has indeed been mentioned, as follows:
[Net/http:setting Custom "Host" Request header doesn ' t has effect #7682] (Net/http:setting Custom "Host" Request header doesn ' t has effect #7682)
And the conclusion is:
I don't think we can safely change the behavior at this point.
It's best to just update the document, removing the ambiguity about the host parameter.
Impact on go implementation of third-party tools
Since the go language has not changed this requirement, in order to adapt to everyone's habits, if necessary, we can do a workaround:
if host := header.Get("Host"); host != "" { req.Host = host}
And it's a common practice for many tools written with go, such as:
Surf
Vegeta
Contact me?
Email:jinsdu@outlook.com
blog:http://www.cnblogs.com/jinsdu/
Github:https://github.com/carlji
Children's shoes, if you think this article is also a good intentions, but also useful, why not order a praise it (⊙o⊙)?