Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: requests seem to ignore Host headers? #29865

Closed
toranger opened this issue Jan 22, 2019 · 4 comments
Closed

net/http: requests seem to ignore Host headers? #29865

toranger opened this issue Jan 22, 2019 · 4 comments

Comments

@toranger
Copy link

What version of Go are you using (go version)?

go1.8.1 linux/amd64

What did you do?

net/http requests seem to ignore Host headers?
`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)
}
`

What did you expect to see?

I want to send to the stable nginx host, but not change the host header which different from the nginx host.

What did you see instead?

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?

@agnivade
Copy link
Contributor

Set the Host field in https://golang.org/pkg/net/http/#Request to override the host value in the URL.

@cuonglm
Copy link
Member

cuonglm commented Jan 22, 2019

Host header is ignored by Request.Write

// Headers that Request.Write handles itself and should be skipped.

@toranger
Copy link
Author

Set the Host field in https://golang.org/pkg/net/http/#Request to override the host value in the URL.

Set the Host field in https://golang.org/pkg/net/http/#Request to override the host value in the URL.
The scene is that I want to send the request with the s3 bucket host such like "BucketName.s3.amazonaws.com" , and send this message to the server which has nginx front which set the location such like "location ~ ^(/test/)(.*)$ ".
I can change the host header by your way, but can not send to the special program on server

@bradfitz
Copy link
Contributor

What @agnivade said:

https://play.golang.org/p/4W_KJGBa77s

It works.

@mikioh mikioh changed the title net/http requests seem to ignore Host headers? net/http: requests seem to ignore Host headers? Feb 26, 2019
@golang golang locked and limited conversation to collaborators Feb 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants