GAE Golang - HTTP JSON RPC call works in the dev_appserver, but not on the App Engine?
-
21-04-2021 - |
سؤال
I'm creating a Go Google App Engine application that will be making HTTP JSON RPC calls to a Bitcoin server. I got my code to work properly on the dev_appserver, but when deployed on GAE, the code seems to not work. I'm using a library available on GitHub, and call it like this:
func GetInfo(id interface{})(map[string]interface{}, os.Error){
resp, err:=httpjsonrpc.Call("user:pass@111.1.1.1:18332", "getinfo", id, nil)
if err!=nil{
log.Println(err)
return resp, err
}
return resp, err
}
Which when called should give:
map[proxy: keypoololdest:1.327368259e+09 blocks:45385 keypoolsize:101 connections:11 version:50200 difficulty:8.88353262 generate:false hashespersec:0 paytxfee:0 balance:0 genproclimit:-1 testnet:true errors:]
But on GAE calling the function seems to be causing an error. What part of the code could be working on dev_appserver, but fail on GAE?
المحلول
You should make you are using urlfetch.Transport
to make HTTP calls in production as described in urlfetch documentation.
Instead of doing:
resp, err := http.Post(address,
"application/json", strings.NewReader(string(data)))
You should be doing:
client := urlfetch.Client(context)
resp, error := client.Post(address,
"application/json", strings.NewReader(string(data)))
As you can see in the implementation, urlfetch.Client
is just a shortcut to construct an http.Client
that uses urlfetch.Transport
.