3

I can use curl to send my request successfully, but golang seems impossible to do.

curl -H 'Host: aaa.com' 'http://bbb.com'

My question is: how I can send request with host which is different with host in url?

0

1 Answer 1

10

Set Request.Host to change the host header sent to the server.

req, err := http.NewRequest("GET", "http://bbb.com/", nil)
if err != nil {
    log.Fatal(err)
}
req.Host = "aaa.com"

Playground example

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers. It's confusing though why setting Host header does not suffice here.
@YanFoto The net/http package uses the Request.Host field to represent the Host header instead of the "Host" member of Request.Header. This special treatment of the header is a bit confusing, but that's the way it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.