Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
net/http: Client following redirects won't preserve Host header #22233
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
referenced this issue
Oct 12, 2017
As mentioned in Brad's comment here, the HTTP RFCs do not specify exactly which headers should be copied. The HTML Fetch Spec does specify which headers are carried through the fetch, but Fetch does not allow setting a custom Host header, so this issue cannot apply. This also implies that we cannot look to browsers for guidance because they cannot run into this issue.
One approach is to copy the original Host header through all redirect requests. This is apparently what Python does, however, there are good arguments that Python's behavior is wrong. What @timonwong proposes is what curl does: copy the Host header only if the location is relative. This seems reasonable to me.
It's also worth mentioning that setting req.Host different from req.URL.Host is a niche use case that should basically never be done unless the caller really knows what they are doing.