1

Golang's package net/http/transport can automatic setup Proxy-Authorization header in

func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (*persistConn, error)

like

proxyURL, _ := url.Parse("http://username:password@example.com")
client      := http.Client{Transport: &http.Transport{Proxy:http.ProxyURL(proxyURL)}}

But I need submit X-Header to proxy server. How Can I custom transport CONNECT method request header?

net/http/transport

Jonathan
  • 11
  • 3

2 Answers2

1

http.Transport has a function which allows you to set some additional headers which will be sent during CONNECT.

Example:

var client http.Client
client.Transport = &http.Transport{
    Proxy: http.ProxyURL(myProxy),
    GetProxyConnectHeader: func(ctx context.Context, proxyURL *url.URL, target string) (http.Header, error) {
        return http.Header{"My-Custom-Header": []string{"My-Custom-Value"}}, nil
    },
}
Rockietto
  • 109
  • 1
  • 11
0

how about this:

// ...
request, err := http.NewRequest("GET", "https://www.google.com", nil)
if err != nil {
  // do something
}
// add header here.
request.Header.Add("X-Header", "xxx")

response, err := client.Do(request)
if err != nil {
  // do something
}
// ...
jsxqf
  • 557
  • 3
  • 13
  • This will not work, because `request.Header` will won't setup to **CONNECT** request. – Jonathan Sep 20 '16 at 16:57
  • I think proxy server can't 'see' the request header when you are using `CONNECT`, check here: https://stackoverflow.com/questions/10369679/do-http-proxy-servers-modify-request-packets – jsxqf Sep 21 '16 at 02:11