2017-04-17 07:48
浏览 23

更改* http.Client传输

The Status Quo

Having picked a side project (building a wrapper around a third party API), I'm stuck. I am using sling to compose my HTTP requests.

So parts of the Client are composed as follows:

type Client struct {
    // some services etc..
    sling *sling.Sling <-- this is initialized with *http.Client

func NewClient(httpClient *http.Client) *Client {
    sling := sling.New().Client(httpClient).Base(BaseURL)


Things I can't wrap my head around

I am following the same principle as go-github and go-twitter that authentication should not be handled by my library, but rather by golangs oauth1/2 package.

As the the API provides application and user level authentication and some of the workflows require initial application level authentication and then user level authentication, my question is, if there is any way to change the *http.Transport in order to change the authentication header on a client basis.

So far, I haven't found a way to do so.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douduxia1551 2017-04-17 09:26

    The http.Client has a Transport field that you could use to "change the authentication header on a client basis" if that's what you want. The Transport field has type http.RoundTripper which is a one method interface, so all you need to do is to define your transport with an implementation of the RoundTrip method.

    type MyTransport struct {
        apiKey string
        // keep a reference to the client's original transport
        rt http.RoundTripper
    func (t *MyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
        // set your auth headers here
        r.Header.Set("Auth", t.apiKey)
        return t.rt.RoundTrip(r)

    Now you can use an instance of this type to set the Transport field on an http.Client.

    var client *http.Client = // get client from somewhere...
    // set the transport to your type
    client.Transport = &MyTransport{apiKey: "secret", tr: client.Transport}

    Depending on how and where from you got the client, it's possible that its Transport field is not yet set, so it might be a good idea to ensure that your type uses the default transport in such a case.

    func (t *MyTransport) transport() http.RoundTripper {
        if t.rt != nil {
            return t.rt
        return http.DefaultTransport
    // update your method accordingly
    func (t *MyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
        // set your auth headers here
        r.Header.Set("Auth", t.apiKey)
        return t.transport().RoundTrip(r)

    It might be worth noting that the Go documentation recommends not to modify the *http.Request inside the RoundTrip method, so what you can do, and what the go-github package you linked to is doing, is to create a copy of the request, set the auth headers on it, and pass that to the underlying Transport. See here:

    解决 无用
    打赏 举报

相关推荐 更多相似问题