Skip to content

net/http: Setting custom "Host" request header doesn't have effect #7682

Closed
@gopherbot

Description

@gopherbot

by mislav.marohnic:

go version: go1.2

The `Header.Add` line in the following code doesn't have any effect:

    client := &http.Client {}
    req, _ := http.NewRequest("GET", "http://127.0.0.1:9292";, nil)
    req.Header.Add("Host", "example.com")
    client.Do(req)

I expected that the server on 127.0.0.1:9292 receives a request with the header
"Host: example.com", but the request was made with "Host:
127.0.0.1:9292" instead.

This does the trick, however:

    req.Host = "example.com"

I was wondering why doesn't the first approach work?

Relevant code:
https://code.google.com/p/go/source/browse/src/pkg/net/http/request.go?name=go1.2#301

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions