robby colvin : google app engine http.defaulttransport

While working on a small project recently that utilizes Go and Google’s App Engine, I ran into the following error:

http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://developers.google.com/appengine/docs/go/urlfetch/overview

Google suggests using the "appengine/urlfetch" package to make requests, but this is not ideal. You are now at the mercy of that implementation and the possibility of being behind the standard version. Additionally, you lose the ability to make requests via third-party packages.

You can work around this by assigning a transport from that package:

client := &http.Client{
    Transport: &urlfetch.Transport{Context: ctx},
}

If you’re using an external package, you can do the same thing as long as you can pass the transport in. I recently used this with tweetlib:

transport := &tweetlib.Transport{
    Config:    tweetlibConfig,
    Token:     token,
    Transport: &urlfetch.Transport{Context: ctx},
}

If the package you’re using doesn’t expose the transport, you’ll need to fork the project and hopefully your patch will be accepted.

I recommend reading the documentation for urlfetch and net/http to better understand how they work.

all posts »