我的urlfetch客戶端在部署到appspot時可以正常工作。但通過代理進行本地測試(dev_appserver.py)有問題。我找不到任何方法爲urlfetch.Transport設置代理。如何通過代理本地測試gae golang urlfetch?
如何在本地測試代理後面的urlfetch?
我的urlfetch客戶端在部署到appspot時可以正常工作。但通過代理進行本地測試(dev_appserver.py)有問題。我找不到任何方法爲urlfetch.Transport設置代理。如何通過代理本地測試gae golang urlfetch?
如何在本地測試代理後面的urlfetch?
這只是一種猜測,但你嘗試setting the proxy variables
在Unix或Windows環境, 之前設置的http_proxy,或ftp_proxy這 環境變量來標識代理服務器的URL啓動的Python翻譯。例如(該 '%' 是命令 提示符):
%HTTP_PROXY = 「http://www.someproxy.com:3128」
%的出口HTTP_PROXY
如果你是使用默認代理然後傳輸被執行爲
var DefaultTransport RoundTripper = &Transport{Proxy: ProxyFromEnvironment}
設置環境變量啓動時應該去解決問題。
另見本的其他問題: How do I configure Go to use a proxy?
的urlfetch包本身不接受代理設置,即使是在發展,因爲它實際上沒有做網址提取本身:它發送到(可能發展)的請求應用程序服務器並要求它執行提取。我沒有的dev_appserver.py
方便的來源,但它應該尊重標準的代理變量:
export http_proxy='http://user:[email protected]:3210/'
如果你這樣做,你開始之前dev_appserver.py
,它可能只是工作。
如果以上不工作,你應該file an issue,然後使用以下解決方法:
func client(ctx *appengine.Context) *http.Client {
if appengine.IsDevAppServer() {
return http.DefaultClient
}
return urlfetch.Client(ctx)
}
這將使用網址抓取API的生產應用服務器,但以其他方式使用標準net/http
客戶端,這does honor的HTTP_PROXY環境變量。
http.DefaultTransport和http.DefaultClient在App Engine中不可用。見https://developers.google.com/appengine/docs/go/urlfetch/overview
在GAE dev_appserver.py測試貝寶的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)
正如你可以看到,圍棋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{}
}
這解決了它給我一起去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")
這對我有用,但我不得不使用調用「client.NewRequest」而不是「http.NewRequest」 –
我有他們設置。這是一款使用瀏覽器ID進行登錄的golang應用程序。感謝您的回覆 – GQ77