GQ7*_*Q77 6 proxy google-app-engine localhost go urlfetch
我的urlfetch客户端在部署到appspot时工作正常.但是通过代理的本地测试(dev_appserver.py)存在问题.我找不到任何方法来为urlfetch.Transport设置代理.
你如何在本地测试代理后面的urlfetch?
http.DefaultTransport和http.DefaultClient在App Engine中不可用.请参阅https://developers.google.com/appengine/docs/go/urlfetch/overview
在GAE dev_appserver.py上测试PayPal OAuth时出现此错误消息(在编译时在生产中工作)
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)
Run Code Online (Sandbox Code Playgroud)
如您所见,Go App Engine打破了http.DefaultTransport(GAE_SDK/goroot/src/pkg/appengine_internal/internal.go,第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{}
}
Run Code Online (Sandbox Code Playgroud)
这通过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")
Run Code Online (Sandbox Code Playgroud)