Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to local test gae golang urlfetch through proxy?

My urlfetch client works fine when deployed to appspot. But local testing (dev_appserver.py) through proxy has issue. I can't find any way to set proxy for urlfetch.Transport.

How do you test urlfetch behind proxy locally?

like image 730
GQ77 Avatar asked Nov 08 '12 21:11

GQ77


1 Answers

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

Got this error message when testing PayPal OAuth on GAE dev_appserver.py (works in production when compiled)

const url string = "https://api.sandbox.paypal.com/v1/oauth2/token"
const username string = "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp"
const password string = "EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp"

client := &http.Client{}        

req, _ := http.NewRequest("POST", url, strings.NewReader("grant_type=client_credentials"))
req.SetBasicAuth(username, password)
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Language", "en_US")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(req)

As you can see, Go App Engine breaks http.DefaultTransport (GAE_SDK/goroot/src/pkg/appengine_internal/internal.go, line 142, GAE 1.7.5)

type failingTransport struct{}
func (failingTransport) RoundTrip(*http.Request) (*http.Response, error) {
    return nil, errors.New("http.DefaultTransport and http.DefaultClient are not available in App Engine. " +
        "See https://developers.google.com/appengine/docs/go/urlfetch/overview")
}

func init() {
    // http.DefaultTransport doesn't work in production so break it
    // explicitly so it fails the same way in both dev and prod
    // (and with a useful error message)
    http.DefaultTransport = failingTransport{}
}

This solved it to me with Go App Engine 1.7.5

    transport := http.Transport{}

    client := &http.Client{
        Transport: &transport,
    }       

    req, _ := http.NewRequest("POST", url, strings.NewReader("grant_type=client_credentials"))
    req.SetBasicAuth(username, password)
    req.Header.Set("Accept", "application/json")
    req.Header.Set("Accept-Language", "en_US")
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
like image 101
Aigars Matulis Avatar answered Oct 15 '22 14:10

Aigars Matulis