GAE Golang – HTTP JSON RPC call works in the dev_appserver, but not on the App Engine?

huangapple go评论72阅读模式
英文:

GAE Golang - HTTP JSON RPC call works in the dev_appserver, but not on the App Engine?

问题

我正在创建一个Go Google App Engine应用程序,它将对Bitcoin服务器进行HTTP JSON RPC调用。我已经成功地在dev_appserver上使我的代码正常工作,但是在部署到GAE上时,代码似乎无法工作。我使用了一个在GitHub上可用的库,并像这样调用它:

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
}

调用时应该返回:

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:]

但是在GAE上调用该函数似乎会导致错误。代码的哪一部分可能在dev_appserver上工作正常,但在GAE上失败?
1: https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29
2: https://github.com/ThePiachu/Go-HTTP-JSON-RPC/blob/master/httpjsonrpc/httpjsonrpcClient.go

英文:

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?
1: https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29
2: https://github.com/ThePiachu/Go-HTTP-JSON-RPC/blob/master/httpjsonrpc/httpjsonrpcClient.go

答案1

得分: 3

你应该在生产环境中使用urlfetch.Transport来进行HTTP调用,具体请参考urlfetch文档

不要使用以下方式:

resp, err := http.Post(address,
    "application/json", strings.NewReader(string(data)))

而应该使用以下方式:

client := urlfetch.Client(context)
resp, error := client.Post(address,
    "application/json", strings.NewReader(string(data)))

如你在实现中所见,urlfetch.Client只是一个快捷方式,用于构建使用urlfetch.Transporthttp.Client

英文:

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.

huangapple
  • 本文由 发表于 2012年1月31日 12:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/9074356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定