比如我在命令行实现
curl --data '{"jsonrpc":"1.0","id":"curltest","method":"getwalletinfo","params":[]}' http://user:123456@127.0.0.1:18332/
返回内容如
{"result":{"walletname":"","walletversion":169900,"balance":0.00652964,"unconfirmed_balance":0.00000000,"immature_balance":0.00000000,"txcount":9,"keypoololdest":1541586836,"keypoolsize":1000,"keypoolsize_hd_internal":1000,"paytxfee":0.00000000,"hdseedid":"903b49b210bd3ef7e36326b34acdadff831ea1e0","hdmasterkeyid":"903b49b210bd3ef7e36326b34acdadff831ea1e0","private_keys_enabled":true},"error":null,"id":"curltest"}
我如何通过go语言来返回相同的内容?http.post函数能否完成,如果能完成需要怎么做?
curl 的--data就是POST的json数据给服务器。所以你go也是提交对应的post请求和数据就可以了
func main() {
url := "http://restapi3.apiary.io/notes"
fmt.Println("URL:>", url)
var jsonStr = []byte(`{"title":"Buy cheese and bread for breakfast."}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
}
奇怪,采纳功能去哪了